Last active
April 20, 2022 11:26
-
-
Save kasir-barati/def7dd7ae60ca1f5ebf6af6d4b13adf3 to your computer and use it in GitHub Desktop.
Postgresql: dump, restore, connect to it, etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Connect to postgres | |
PGPASSWORD="pass" psql -h localhost -p 5432 -U user -d db-name | |
# You can also use .pgpass, TBH it does not work in my experience. BTW this is much much safer from point of security - No track in terminal history. | |
touch ~/.pgpass | |
# Replace its values: | |
db_host:db_port:db_name:db_user:db_pass | |
# Dump from postgres | |
# Doc: https://www.postgresql.org/docs/14/app-pgdump.html | |
# You can ignore using PGPASSWORD, instead you can pass it when it asks you. In this way your password will be safer. | |
PGPASSWORD="pass" pg_dump -h localhost -p 5432 -U user -F c -b -v -f filename-it-can-be-db-name.dump db-name | |
# Restore from .dump file | |
# Doc: https://www.postgresql.org/docs/14/app-pgrestore.html | |
# This restore won't restore password for the database. BTW we can change it as we want as I did in First option. | |
# IDK what can I do about it. If password is important to you just use the second option. | |
# First option to restore: | |
pg_restore --clean --create --dbname=db-name --verbose --host=localhost --port=5432 --user=postgres filename.dump | |
psql -U postgres | |
\password 123456 | |
# Now you can connect to database via this connections string: | |
# postgresql://postgres:123456@localhost:5432/db-name | |
# Second option to resotore: | |
pg_restore --no-privileges --no-owner --dbname="postgresql://username:password@host:port/database" file.sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment