Skip to content

Instantly share code, notes, and snippets.

@ardian
Forked from vielhuber/script.sh
Last active March 20, 2024 04:31
Show Gist options
  • Save ardian/d0a888f31a6b192fb7acb9f7eedbc58d to your computer and use it in GitHub Desktop.
Save ardian/d0a888f31a6b192fb7acb9f7eedbc58d to your computer and use it in GitHub Desktop.
PostgreSQL: Backup and restore pg_dump with password on command line #sql
# 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 -h localhost -U postgres -d database < file.sql
## Import dump
psql -h hostname -d databasename -U username -f file.sql
## Asign user to db
ALTER DATABASE dbname OWNER TO dbuser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment