-2

I have a Docker container with PostgreSQL and I want to do a pg_dump of my database. I tried with the following code:

client = docker.from_env()
postgres = client.containers.get('my_postgres')
command = 'pg_dump --dbname=postgresql://my_user:mypassword@localhost:5432/my_db -Fc > /var/lib/postgresql/data/dumps/test.sql'
log = postgres.exec_run(command, stdout=True, stderr=True)
print(log[1])

But I get this error:

b'pg_dump: too many command-line arguments (first is ">")\nTry "pg_dump --help" for more information.\n'

Any thoughts?

1 Answers1

0

You probably need to make a shell command your executable and then pass your whole command to that shell, like this:

command = "sh -c 'pg_dump --dbname .....'"

See this question

  • Spot on. Worked like a champ. May I ask: why are there two set of brackets? Or in other words, why is sh -c separated from the pg_dump part? Thanks – user18140022 Mar 15 '22 at 13:49