92

I would like to have a way to enter into the Postgresql container and get a data dump from it.

11 Answers 11

108

Use the following command from a UNIX or a Windows terminal:

docker exec <container_name> pg_dump <schema_name> > backup

The following command will dump only inserts from all tables:

docker exec <container_name> pg_dump --column-inserts --data-only  <schema_name> > inserts.sql
Sign up to request clarification or add additional context in comments.

5 Comments

where you specify the user and password of the user?
@VaTo you can specify the user like this: docker exec <container_name> pg_dump -U <user> --column-inserts --data-only <schema_name> > inserts.sql
If you dont have the PGUSER set in your container you need to export PGUSER=postgres to make it work (Testet in postgres 9.6).
This also works from windows cmd
If you need to hand in a password, run the identical command above but add "-it" after the docker exec (i.e. "docker exec -it ..."). After entering this command you will get a blank cursor. Type in your database password and hit enter again. It will now execute the pd_dump. Note: i stands for interactive, so adding this flag allows you to input your password when it is required by pg_dump. Unfortunately you will not see a "password" prompt, but if you just type in your password and hit enter, it will work.
37

I have container named postgres with mounted volume -v /backups:/backups

To backup gziped DB my_db I use:

docker exec postgres pg_dump -U postgres -F t my_db | gzip >/backups/my_db-$(date +%Y-%m-%d).tar.gz

Now I have

user@my-server:/backups$ ls
my_db-2016-11-30.tar.gz

7 Comments

I'm getting a no such file or directory: /backups/my_db-2017-03-19.tar.gz, do you have an idea why ?
I managed to work around it with docker exec -t postgres bash -c 'pg_dump -U postgres -F t my_db | gzip >/backups/my_db-$(date +%Y-%m-%d).tar.gz'
Maybe you don't have /backups dir and/or it's not mounted?
It was mounted. The problem seems to come from docker not understanding the command: stackoverflow.com/questions/37929190/…
Minor note: This isn't actually doing a tar gz, it's doing just a gz but you're naming the SQL file with the extension tar and then gzipping it. If you gunzip it, you get an unreadable file called my_db-date.tar, but if you rename the .tar to .sql, it's actually an SQL file. You should probably make it output as just "filename.gz" or use tar -cz if you actually want a .tar.gz file extension
|
22

Although the mountpoint solution above looked promising, the following is the only solution that worked for me after multiple iterations:

docker run -it  -e PGPASSWORD=my_password postgres:alpine  pg_dump  -h hostname -U my_user my_db > backup.sql

What was unique in my case: I have a password on the database that needs to be passed in; needed to pass in the tag (alpine); and finally the hosts version of the psql tools were different to the docker versions.

1 Comment

Similarly to previous answer, it works also docker exec -e PGPASSWORD=mypass container pg_dump -U myuser mydb > file.bkup.sql
7

This one, using container_name instead of database_scheme's one, works for me:

docker exec {container_name} pg_dump -U {user_name} > {backup_file_name}

In instance, for me, database name, user and password are supposed declared in docker-compose.yaml

I wish it could help someone

Comments

6

for those who suffered with permissions, I used this following command with success to perform my dump:

docker exec -i MY_CONTAINER_NAME /bin/bash -c "PGPASSWORD=MY_PASSWORD pg_dump -Fc -h localhost -U postgres MY_DB_NAME" > /home/MY_USER/db-$(date +%d-%m-%y).backup

Comments

2

This will mount the pwd and include your environment variables

docker run -it --rm \
--env-file <(env) \
-w /working \
--volume $(pwd):/working \
postgres:latest /usr/bin/pg_dump -Fc -h localhost -U postgres MY_DB_NAME" > /working/db-$(date +%d-%m-%y).backup

Comments

2

See the PostgreSQL Documentation for Backup and Restore. As others have described, use docker exec to run commands on containers, in this case either pg_dump or pg_dumpall to create dump files. Write them to a docker volume to prevent increasing the container size and provide access to the dump from outside the container.

TLDR

docker exec <container_name> pg_dump [-U db_user] -f [filepath] <db>

e.g.

docker exec db pg_dump -U admin -f /db-backups/db.pg_dump.bak nextcloud

Explanation

Although output redirection is ubiquitous throughout the documentation, you will have trouble with it when using docker exec.

docker exec db pg_dump db > db.pg_dump.bak

What you want to happen

docker exec db (pg_dump db > db.pg_dump.bak)

What is actually happening

(docker exec db pg_dump db) > db.pg_dump.bak

I had trouble trying to use shell quoting to fix this, maybe because of how it is treated in the container. Fortunately, we don't have to use output redirection at all. The man page for pg_dump documents a -f option that takes the destination instead. This avoids the problem entirely.

Comments

1

Another workaround method is to start postgre sql with a mountpoint to the location of the dump in docker.

like docker run -v <location of the files>. Then perform a docker inspect on the docker running container

docker inspect <image_id>

you can find "Volumes" tag inside and a corresponding location.Go to the location and you can find all the postgresql/mysql files.It worked for me.Let us know if that worked for you also.

Good luck

Comments

1

At-least this's what worked for me

docker exec "container name/id" pg_dump -U "DB username" -d "db_name" > exported_file_name.sql
ie: docker exec 9cddbbf7c279 pg_dump -U odoo16 -d SeattleMobi > dump.sql

Comments

0

To run the container that has the Postgres user and password, you need to have preconfigured variables as container environment variable. For example:

docker run -it --rm --link <container_name>:<data_container_name> -e POSTGRES_PASSWORD=<password> postgres /usr/bin/pg_dump -h <data_container_name> -d <database_name> -U <postgres_username> > dump.sql

1 Comment

--link is a legacy feature.
0

I use this script:

docker exec postgres-1 pg_dump -U postgres --column-inserts --data-only  postgres > inserts.sql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.