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 :)