In this short article, we would like to show how to make Postgres database backup from the command line under Linux.
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 (
pg_dump -c -h localhost -p 5432 -U my-username -W my-database > /path/to/backup.sql
-ccauses adding clean queries on the database to the generated dump,
5432should be used according to Postgres server address (
my-usernameshould be replaced with current database credentials,
-Wcauses displaying propmpt to type the password for
my-databaseindicates the name of the database that we want to copy,
/path/to/backup.sqlshould 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