Skip to content

Instantly share code, notes, and snippets.



Forked from vielhuber/
Created Jul 11, 2018
What would you like to do?
PostgreSQL: Backup and restore pg_dump with password on command line #sql
# best practice: linux
nano ~/.pgpass
chmod 0600 ~/.pgpass
# best practice: windows
edit %APPDATA%\postgresql\pgpass.conf
# linux
PGPASSWORD="password" pg_dump -h host -p port -U username database > file.sql
# windows
PGPASSWORD=password&& pg_dump -h host -p port -U username database > file.sql
# alternative
pg_dump --dbname=postgresql://username:password@host:port/database > file.sql
# restore
psql -U postgres database < file.sql
# backup and restore
PGPASSWORD=password && pg_dump -h -p 5432 -U username database > tmp.sql
psql -U postgres -d database -c "drop schema public cascade; create schema public;"
psql -U postgres -d database -1 -f tmp.sql
rm tmp.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment