Languages
[Edit]
EN

Postgres - database backup

7 points
Created by:
martineau
1140

In this short article, we would like to show how to make Postgres database backup from the command line under Linux. 

Quick solution:

pg_dump -h localhost -p 5432 -U my-username -W my-database > /path/to/backup.sql

Note: command will ask us to type the current username password.

 

To include SQL commands to clean (drop) databases before recreating them, use the following command (-c parameter):

pg_dump -c -h localhost -p 5432 -U my-username -W my-database > /path/to/backup.sql

Where:

  • -c causes adding database clean queries to the generated dump,
  • localhost and 5432 should be used according to Postgres server address (host and port),
  • my-username should be replaced with current database credentials,
  • -W causes displaying propmpt to type my-usernames' password,
  • my-database indicates copied database,
  • /path/to/backup.sql should be replaced with destinated backup file location.
Note: do not forget to check if Postgres is installed or run the installation command:
sudo apt install postgresql postgresql-contrib - for Debian / Ubuntu Linuxes

Alternative titles

  1. Postgres - database backup from command line
  2. Postgres - database dump
  3. Postgres - database dump from command line
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join