1

We use Postgres and Flask for our website, and we use the production database dump locally pretty often. To get a fresh dump, I use a remote desktop connection (RDC) to connect to pgAdmin then use RDC again to copy .bak file from server and save it locally. Likewise, I use a local instance of pdAdmin to restore the database state from the backup.

My manager asked me to automate this process to use production database each time when a local Flask instance is launched. How can I do that?

Nikolay Shebanov
  • 1,363
  • 19
  • 33
Minh Tran
  • 11
  • 1
  • 1
  • 3

1 Answers1

0

You could write a shell script that dumps the database to a local file using pg_dump, then use pg_restore to build a new local database from that dump. You could probably even just pass the output from pg_dump to pg_restore... something like

pg_dump --host <remote-database-host> --dbname <remote-database-name> --username <remote-username> > pg_restore --host <local-database-host> --username <local-username>

To get your password into pg_dump / pg_restore you'll probably want to use a .pgpass file, as described here: How to pass in password to pg_dump?

If you want this to happen automatically when you launch a Flask instance locally, you could call the shell script from your initialization code using a subprocess call if a LOCAL_INSTANCE environment variable is set, or something along those lines

zyd
  • 833
  • 7
  • 16
  • thanks, but I have some other question about this. I tried to import pg_dump from sh, but as I know sh is not support for window. So do you have any document about subprocess – Minh Tran Feb 17 '21 at 02:22