Postgresql backup and restore with Powershell

Hi,

I am trying to backup and restore my running Postgres 9.6 DB container using Powershell.
I am able to get the backup working
docker exec -u postgres postgres_postgresdb_1 pg_dump -Fc mydb > mydb_0.1.0.dmp
But when trying to restore
docker exec -u postgres postgres_postgres_1 pg_restore -C -d postgres < mydb_0.1.0.dmp

I get the error message from Powershell that ‘<’ isn’t a supported operator

The ‘<’ operator is reserved for future use.
+ CategoryInfo : ParserError: (: ) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported

So I tried to pipe it with get-content instead
Get-Content mydb_0.1.0.dmp | docker exec -u postgres postgres_postgres_1 pg_restore -C -d postgres

But it gets stuck and does nothing…
Anybody was able to do that on Windows?

The problem is that your command (that you want run in the linux container) is interpreted by powershell. Either use an interactive exec (docker exec -ti) or try to place double quotes around the command you want to run in the exec session.

I tried your solution with -ti and/or using double quotes. It does not work and always throws an error:

PS C:\Users\usr\Documents\WORKSPACE> docker exec -it docker-postgres-db "pg_restore --clean --if-exists --username=my_username --no-owner --verbose --dbname=my_db < dump_db"
OCI runtime exec failed: exec failed: unable to start container process: exec: "pg_restore --clean --if-exists --username=my_username --no-owner --verbose --dbname=my_db < dump_db": executable file not found in $PATH: unknown

PS C:\Users\usr\Documents\WORKSPACE> dir

    Directory: C:\Users\usr\Documents\WORKSPACE

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          09.09.2022    12:46           1117 docker-compose.yaml
-a---          12.09.2022    03:08     1459828400 dump_db
-a---          12.09.2022    03:08     1456679531 dump_db.zst
-a---          12.09.2022    11:15           1118 import.ps1