Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup/restore postgres in docker container
Backup:
docker exec -t -u postgres your-db-container pg_dumpall -c > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
Restore:
cat your_dump.sql | docker exec -i your-db-container psql -Upostgres
@tlvenn

This comment has been minimized.

Show comment
Hide comment
@tlvenn

tlvenn Apr 5, 2017

As explained in this article: http://durandom.de/docker/postgres/2016/12/20/pg_dump/ you should not pass -t to docker exec to backup your DB.

tlvenn commented Apr 5, 2017

As explained in this article: http://durandom.de/docker/postgres/2016/12/20/pg_dump/ you should not pass -t to docker exec to backup your DB.

@EvAlex

This comment has been minimized.

Show comment
Hide comment
@EvAlex

EvAlex Jun 3, 2017

Missing whitespace in restore command between "-U" and "postgres"

EvAlex commented Jun 3, 2017

Missing whitespace in restore command between "-U" and "postgres"

@ChunAllen

This comment has been minimized.

Show comment
Hide comment
@ChunAllen

ChunAllen May 14, 2018

To restore your dump file with specified user and database_name

cat your_dump.sql | docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

ChunAllen commented May 14, 2018

To restore your dump file with specified user and database_name

cat your_dump.sql | docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

@Almaral-Engineering

This comment has been minimized.

Show comment
Hide comment
@Almaral-Engineering

Almaral-Engineering Jul 31, 2018

I am getting a lot of these when I try to restore:
ERROR: constraint "something" for relation "something" already exists

Almaral-Engineering commented Jul 31, 2018

I am getting a lot of these when I try to restore:
ERROR: constraint "something" for relation "something" already exists

@romulo-soares

This comment has been minimized.

Show comment
Hide comment
@romulo-soares

romulo-soares Aug 15, 2018

Ao fazer isso:
cat your_dump.sql | docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

Obtive o seguinte erro:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/banco/exec: dial unix /var/run/docker.sock: connect: permission denied

romulo-soares commented Aug 15, 2018

Ao fazer isso:
cat your_dump.sql | docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

Obtive o seguinte erro:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/banco/exec: dial unix /var/run/docker.sock: connect: permission denied

@GMartinez-Sisti

This comment has been minimized.

Show comment
Hide comment
@GMartinez-Sisti

GMartinez-Sisti Sep 4, 2018

@romulo-soares You need to use sudo before docker if you are not root (tens de usar sudo se não estás logado com root):

cat your_dump.sql | sudo docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

GMartinez-Sisti commented Sep 4, 2018

@romulo-soares You need to use sudo before docker if you are not root (tens de usar sudo se não estás logado com root):

cat your_dump.sql | sudo docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment