Where Postgres database files are saved in ubuntu?

Here's how I located the directory of my Postgres database files in Ubuntu:

Run the command below in your terminal to switch user to postgres user:

su - postgres

It will request for the postgres user password which you set up when you were setting up PostgreSQL on your machine.

Next, you will need to login into the psql terminal/prompt. This can be accomplished by running the code below:

psql

It will also request for the postgres password which you set up when you were setting up PostgreSQL on your machine.

If you are successfully logged in, you will see a prompt similar to this:

psql (11.5 (Ubuntu 11.5-3.pgdg18.04+1), server 10.10 (Ubuntu 10.10-1.pgdg18.04+1))
Type "help" for help.

postgres=#

At this point you will execute the query below to display the directory where Postgres Database files are stored on your Linux macchine:

SHOW data_directory;

This should display an output similar to this:

data_directory        
-----------------------------
/var/lib/postgresql/10/main
(1 row)

You can now locate the Postgres Database files by navigating the directory that was displayed.

However, the PostgreSQL configuration directory in Ubuntu is located in /etc/postgresql/10/main. Take note that 10 is the version of my PostgreSQL installation on my server. Your version might be 9.5, 11 or 12 or any other version. Run the command psql --version to confirm your PostgreSQL version.

Run the command below to navigate to the directory:

cd ~
cd /etc/postgresql/10/main

That's all.

I hope this helps


In the postgres prompt, just execute this query:

SHOW data_directory;

Check also the Ubuntu manual: https://help.ubuntu.com/10.04/serverguide/C/postgresql.html