28 October 2026 · One command a day
How do I back up a Postgres database from the command line?
pg_dump copies one PostgreSQL database into a file. You can keep that file, move it or restore it later. Use it before an upgrade or a risky migration, or as part of a nightly backup. It works while the database is live and does not block normal reads and writes.
I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.
The command
sudo -u postgres pg_dump dbname > dbname.sqlsudo -u postgres runs the command as the postgres system user. On Ubuntu, that user can connect to the database without a password. pg_dump is the backup tool. dbname is the name of the database you want to copy. > dbname.sql sends the output to a file in your current directory. Your own shell handles that redirect, so the file belongs to you, not to postgres. If your app connects with its own database user and password, you can run pg_dump -U appuser -h 127.0.0.1 dbname > dbname.sql instead and type the password when asked. If you have used the MySQL equivalent, mysqldump, this works the same way.
What you will see
$ sudo -u postgres pg_dump dbname > dbname.sql
$ ls -lh dbname.sql
-rw-rw-r-- 1 user user 48M Oct 28 02:15 dbname.sql
$ grep "dump complete" dbname.sql
-- PostgreSQL database dump completeWhen pg_dump succeeds it prints nothing. Any text on screen is an error, usually a wrong database name or a failed login. Use ls -lh to check that the file exists and has a sensible size. A file of a few hundred bytes means something went wrong. The last check looks for the line pg_dump writes when it finishes. If that line is missing, the dump was cut short.
When to use it
- Before you run database migrations as part of a deploy, so you can roll back if they fail.
- Before you upgrade PostgreSQL or move an app to another server.
- Every night, run from a crontab entry with the date in the file name, for example
dbname-$(date +%F).sql. In a crontab line, write%as\%.
Watch out for
pg_dump backs up one database. It does not include database users (roles) or their passwords. Those belong to the whole server, not to any one database. If you restore your dump on a new server, the tables come back but the app may not be able to log in. Back up roles separately with sudo -u postgres pg_dumpall --globals-only > globals.sql. Also, keep a copy off the server. A backup on the same disk will not help if the disk fails.
Questions people ask
How do I restore a pg_dump .sql file?
Create an empty database with sudo -u postgres createdb dbname, then run sudo -u postgres psql dbname < dbname.sql. A plain .sql file is restored with psql, not pg_restore.
Can I compress the backup as I make it?
Yes. Run sudo -u postgres pg_dump dbname | gzip > dbname.sql.gz. Or use -Fc to get pg_dump's compressed custom format, which you restore with pg_restore.
Running your own database backups on your own schedule is something you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.
Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.
Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.
Affiliate link.