Skip to content

Instantly share code, notes, and snippets.

@itSQualL
Last active October 1, 2021 16:08
Show Gist options
  • Save itSQualL/dc17356ba05ce3ef881ba5ff1bc4dfbc to your computer and use it in GitHub Desktop.
Save itSQualL/dc17356ba05ce3ef881ba5ff1bc4dfbc to your computer and use it in GitHub Desktop.
How to backup and restore postgres from docker

Docker & PostgreSQL

How to backup PostgreSQL database from Docker container

  1. Go to the directory with docker-compose file.

  2. Execute the next command to get db dump (note that the next command is an example for actual docker-compose config):

docker-compose run postgres pg_dump -h postgres -U postgres db_development > db_development.dump

How to restore PostgreSQL database from Docker container

  1. In case you already have an existent db: docker-compose run <volume> bundle exec rake db:drop
  2. Create the db: docker-compose run <volume> bundle exec rake db:create
  3. Import the dump to the postgres volume:
cat <dump> | docker-compose run postgres psql -h postgres -U postgres -d <db_name>

or, for native compressed sql files

docker-compose run postgres pg_restore -U postgres -h postgres --verbose -d db_development --clean < dumpfile.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment