Postgresql – Location of postgres db file for backup

backuppostgresql

I would like to make a backup of a postgres database file that I created on Ubuntu 16.04.

I cannot find the file. Some other sources indicate it should be under:

/var/lib/postgresql/[version]/data

When I run show data_directory inside of postgres it shows:

/var/lib/postgresql/9.5/main

However there is no data folder there.

Best Answer

To find out where the PostgreSQL data folder is located, just run SQL query:

show data_directory;

You can find more details here

However, note

File System Level Backup are faster but large in size. The worst thing is that to perform a database's backup, PostgreSQL Server required to be stopped. More information about the restrictions of this operation can be found in the official documentation. On my own I will add that from such backups, as a rule, there is more harm than good.

The main method for creating backups in PostgreSQL is the pg_dump utility.

pg_dump dbname > dbname.dump

Opposite file-level backup, a backup via the pg_dump tool does not require stopping the server, it is easy to automate and does not require access to the server file system (it can be performed remotely).