Skip to content

Instantly share code, notes, and snippets.

@rseabrook
Created February 21, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rseabrook/e5104b01c7b420c9c01e522ddeb024a9 to your computer and use it in GitHub Desktop.
Save rseabrook/e5104b01c7b420c9c01e522ddeb024a9 to your computer and use it in GitHub Desktop.
How to copy data from production postgresql database to local database running in Docker

How to copy your production Postgresql database to your local environment running in Docker

I ran into this issue specifically in the context of a Django application deployed on Render, but these steps should work for any stack running locally in a Docker container.

Why would you want to copy your production database to your local environment?

I encountered an error in production that did not occur in my local environment. I needed to debug the issue locally, but I needed a copy of the production database to properly debug it.

How to get a copy of your production database

My application is deployed on Render, which takes daily backups automatically. The Render docs explain how the backup files are created. I was able to just download the .sql.gz file of the latest backup through the Render dashboard.

How to populate a local postgres database from the backup file

If the database is NOT running in a container, you can do something like this:

psql -h <hostname> -p <port> -U <username> -d <database> -f /path/to/local/backup_file.sql

If your database is running inside of a docker container, you need to either:

  • copy the file into your docker container
  • stream the file contents to the docker command

You can copy the file into docker using the docker cp command followed by the psql command above, but I chose to stream the contents to the command instead. This is a simple one-liner.

cat /path/to/local/backup_file.sql | docker exec -i <container name> psql -h <hostname> -p <port> -U <username> -d <database>

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