Operations related to database configuration, connecting, data management, and data querying.
Databases
Examples in
Shell
Extract a PostgreSQL database into a script file or other archive file.
More information:
https://www.postgresql.org/docs/current/app-pgdump.html .
# Dump database into a SQL-script file:
pg_dump {{db_name}} > {{output_file.sql}}
# Same as above, customize username:
pg_dump -U {{username}} {{db_name}} > {{output_file.sql}}
# Same as above, customize host and port:
pg_dump -h {{host}} -p {{port}} {{db_name}} > {{output_file.sql}}
# Dump a database into a custom-format archive file:
pg_dump -Fc {{db_name}} > {{output_file.dump}}
# Dump only database data into an SQL-script file:
pg_dump -a {{db_name}} > {{path/to/output_file.sql}}
# Dump only schema (data definitions) into an SQL-script file:
pg_dump -s {{db_name}} > {{path/to/output_file.sql}}
createuser user_name
# Dump to sql file.
pg_dump -h host -U user dbname > dump.sql
# Dump using postgres compressed mode.
pg_dump -h host -U user -Fc dbname > dump.custom
# Dump to sql file compressed with gzip.
pg_dump -h host -U user dbname | gzip > dump.sql.gz
# Dump remote database through ssh.
ssh user@host -i sshkey "pg_dump -h dbhost -U dbuser dbname" > dump.sql
cat dump.sql | psql -U dbuser dbname