206

I forgot how I started PostgreSQL the last time (it was months ago) and I don't remember where the data directory is located. The postgres command seems to require the location of the data directory.

I'm on MacOsX if that helps.

/usr/local/postgres doesn't exist on my Mac.

Using the answers provided below, I found that it was here:

/usr/local/var/postgres
2
  • in many cases it is sufficient to switch to the postgres user sudo su - postgres and check the home directory path using pwd. Commented Jan 7, 2023 at 21:11
  • Use SELECT current_setting('data_directory') AS pgdata_path; Commented Nov 15, 2023 at 21:45

17 Answers 17

246

If you can connect to the database with superuser access, then

SHOW data_directory;

is the shortest way.

If the server is not running and you forgot where the data directory was, then you really have to guess yourself. An operating system specific convention or the shell history might provide hints.

1
  • 10
    if it's not running, try running a find for postgresql.conf, which is usually in the data directory, or will have information about the data directory.
    – xzilla
    Commented Dec 12, 2011 at 20:15
48

You can also query for it

select setting from pg_settings where name = 'data_directory';
1
  • 2
    Returned 0 rows for pgsql v.10 Commented Nov 25, 2019 at 22:52
42

pg_config shows a lot of information including the data directory:

CONFIGURE = '--disable-debug' '--prefix=/usr/local/Cellar/postgresql/9.3.2' 
            '--datadir=/usr/local/Cellar/postgresql/9.3.2/share/postgresql' 
            '--docdir=/usr/local/Cellar/postgresql/9.3.2/share/doc/postgresql' 
            '--enable-thread-safety' '--with-bonjour' '--with-gssapi' '--with-krb5' 
            '--with-ldap' '--with-openssl' '--with-pam' '--with-libxml' '--with-libxslt'
            '--with-ossp-uuid' '--with-python' '--with-perl' '--with-tcl' 'CC=clang' 
            'CFLAGS=-I/usr/local/Cellar/ossp-uuid/1.6.2/include' 
            'LDFLAGS=-L/usr/local/Cellar/ossp-uuid/1.6.2/lib' 'LIBS=-luuid'

This might be a brew-specific thing, though; without brew I don't know what it shows.

3
  • 2
    I'm using brew too. As of now, the datadir listed in pg_config --configure is not the same as the one brew info postgresql recommends (which is $(brew --prefix)/var/postgres and is not a symlink to the one listed). It seems the formula is a bit lax on using config flags properly.
    – Vaz
    Commented Aug 12, 2015 at 21:52
  • Note for Ubuntu/Debain: pg_config is not installed with the postgresql-<ver> apt package, but requires also postgresql-server-dev-<ver>
    – pscl
    Commented Apr 20, 2018 at 20:36
  • @PSCL True, but OP was under OS X. Commented Apr 20, 2018 at 20:38
26

On Ubuntu\Debian, try the pg_lsclusters command.

On my machine:

pg_lsclusters 
Version Cluster   Port Status Owner    Data directory             Log file
8.4     main      5432 online postgres /home/adam/db/main/data    /home/adam/db/main/log
0
12

If you can't log in (eg, forgot your password) but the service is running, as MacOS X is a posix varient, you can always check ps to see if it's being passed in as an argument :

On a linux box with two postgres instances running:

-bash-3.2$ ps ax | grep postgres | grep -v postgres:
 4698 ?        S      9:59 /opt/PostgreSQL/8.4/bin/postgres -D /opt/PostgreSQL/8.4/data
 6115 ?        S      8:16 /opt/PostgreSQL/8.4/bin/postgres -D /opt/PostgreSQL/8.4/data_sums
30694 pts/9    S+     0:00 grep postgres
9

Modern versions of the PostgreSQL desktop client for OSX have a convenient "Server Settings" dialog that will allow you not just to list the data directory but open it directly.

Very useful if you experience a random machine crash that leaves behind a lock file.

enter image description here

0
4

Open postgresql.conf, go to the line:

data_directory='/var/lib/postgresql/main/'

is your answer.

1
  • 4
    This line is commented out in my PostgreSQL.conf.
    – ijt
    Commented Oct 12, 2021 at 22:55
4

Generalizing from the 2 top answers:

To show the data directory directly at the command-line:

psql -U postgres -tA -c "SHOW data_directory;"

To also see all other defined paths to files and directories:

psql -U postgres -c "SELECT name, setting FROM pg_settings WHERE setting LIKE '/%';"

Sample output:

          name           |                 setting                  
-------------------------+------------------------------------------
 config_file             | /etc/postgresql/9.6/main/postgresql.conf
 data_directory          | /mnt/pg_ssd_500/9.6/main
 external_pid_file       | /var/run/postgresql/9.6-main.pid
 hba_file                | /etc/postgresql/9.6/main/pg_hba.conf
 ident_file              | /etc/postgresql/9.6/main/pg_ident.conf
 ssl_cert_file           | /etc/ssl/pg_server.cer
 ssl_key_file            | /etc/ssl/private/pg_server.key
 stats_temp_directory    | /var/run/postgresql/9.6-main.pg_stat_tmp
 unix_socket_directories | /var/run/postgresql
(9 rows)

(On Windows, replace LIKE '/%' with LIKE '%/%' because the path starts with a drive letter)

See also https://www.postgresql.org/docs/current/storage-file-layout.html which gives more details.

For example:

For each database in the cluster there is a subdirectory within PGDATA/base, named after the database's OID in pg_database

So this will give the specific data directories under $PGDATA/base for each database:

psql -U postgres -c "SELECT oid, datname FROM pg_database;"
2

On Windows Server 2012, the command pg_config did not reveal the data directory for some crazy reason. But I found it by looking at the file C:\Program Files\PostgreSQL\9.4\pg_env.bat, which contained this line:

@SET PGDATA=E:\POSTGRESQL

That was the answer I needed.

1

On OS X 10.8 and 10.9 (not 10.10) with server app installed, in a terminal window, type

sudo serveradmin settings postgres

On OS X 10.9 Mavericks, this is the output I get from that, which includes the data directory.

postgres:log_connections = "on"
postgres:unix_socket_directory = "/private/var/pgsql_socket"
postgres:listen_addresses = "127.0.0.1,::1"
postgres:unix_socket_group = "_postgres"
postgres:log_statement = "ddl"
postgres:log_line_prefix = "%t "
postgres:unix_socket_permissions = "0770"
postgres:log_lock_waits = "on"
postgres:logging_collector = "on"
postgres:log_filename = "PostgreSQL.log"
postgres:dataDir = "/Library/Server/PostgreSQL/Data"
postgres:log_directory = "/Library/Logs/PostgreSQL"

I just tried this in Yosemite (OS X 10.10) and postgres is no longer listed as a service under serveradmin (although postgres is installed).

sudo serveradmin list

does not list postgres

1

As pointed out, one could also use ps to capture the data location such as:

ps -e |egrep postgres|egrep -E '[[:blank:]]+\-D'| perl -pe 's/^.*[[:blank:]]+-D[[:blank:]]+(\S+).*?$/$1/'.

This would work most of the time. It would return something like /usr/local/pgsql/data or wherever else it was specified in the parameters of PostgreSQL at launch.

1

On Fedora and probably several other Linux distros you can change to the postgres user and read the PGDATA environment variable. E.g.:

sudo su - postgres
echo ${PGDATA}
/var/lib/pgsql/12/data
1

For M1 Macs with Postgres installed via Homebew, try:

/opt/homebrew/var/[postgresql@12 or other version]
0

How about using the standard utility pg_config such as pg_config --bindir One could also get the the configure parameters used to compile PostgreSQL and parse them with pg_config --configure

2
  • --bindir shows the location of the executables, not the data directory. --configure is OK, even though one has to parse the output a bit. Commented Oct 28, 2017 at 21:49
  • Yes that is true and unfortunately pg_config has no parameter that would reveal the datadir. When I wrote this, I thought that at least it gives something close, assuming the datadir is not in a special location. datadir would be relative then to the bindir such as ../data
    – Jacques
    Commented Oct 29, 2017 at 22:32
0

Based on the answer by @Matthew Mark Miller, on macOS 10.14 using Postgres 11 it is located at:

~/Library/Application Support/Postgres
0

Since there are three different libraries in Mac, developers are usually confused with the three scenarios. However, you can find out your PostgreSQL directory with one of the two methods on the Mac Computer.

1. Directory--Absolute Path

1). Scroll up your mouse to the left top of Mac Screen

2). Click the menu "Go"

3). Select the "Go to Folder..."

4). Input the path

/Users/username/Library/Application Support/Postgres

Please change "username" to your name such as "jack" listed as follows.

/Users/jack/Library/Application Support/Postgres

5). Choose the button "Go" to the directory of Postgres.

You will eventually find out Postgres.

2. Find the directory step by step

1). Scroll up your mouse to the left top of Mac Screen.

2). Click the menu "Go".

3). Select the button "Library"

4). Double click the file holder "Application Support".

5). Find out "Postgres" according to the alphabetic order.

Cheers,

Mike

-1

on macOS Big Sur version 11.5.2

postgresql 10.17 installed with homebrew /opt/pgdata

Not the answer you're looking for? Browse other questions tagged or ask your own question.