How To Back up Your Self-Managed PostgreSQL Database?

If you run a self-managed PostgreSQL database on a Linux server, you will eventually need to back up your data. 

To proceed, you will need to have:

  • A database user with all privileges on the database you want to back up.
  •  The database name

Now you can run the following command:

PGPASSWORD="<password>" pg_dump -h localhost -U <user> <db name> > db__$(date -d "today" +"%d_%m_%Y_%H_%M_%S").bak

You need to replace:

  • <password> with the password of the database user.
  • <user> with the database username
  • <db name> with the database name

Optionally, you can also replace the part db__$(date -d "today" +"%d_%m_%Y_%H_%M").bak with the desired output name.  However, with the current setup, you get a backup file with the current timestamp.

That is it :)

About The Author
Author Avatar
Mohamed AbdelGawad

IT Automation Specialist

Hi and welcome to my blog where I like to record and document the technical issues I tackle and the solutions I apply. Hopefully, it will make someone's life easier.

I'd love to hear your thoughts...

Get in Touch.

Whether you have a question or just want to say hi, I’ll try my best to get back to you as soon as possible.