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
17 Answers 17
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.
-
10if 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– xzilla2011年12月12日 20:15:56 +00:00Commented Dec 12, 2011 at 20:15
You can also query for it
select setting from pg_settings where name = 'data_directory';
-
2Returned 0 rows for pgsql v.10alexkovelsky– alexkovelsky2019年11月25日 22:52:04 +00:00Commented Nov 25, 2019 at 22:52
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.
-
2I'm using brew too. As of now, the datadir listed in
pg_config --configure
is not the same as the onebrew 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– Vaz2015年08月12日 21:52:02 +00:00Commented Aug 12, 2015 at 21:52 -
Note for Ubuntu/Debain: pg_config is not installed with the
postgresql-<ver>
apt package, but requires alsopostgresql-server-dev-<ver>
pscl– pscl2018年04月20日 20:36:46 +00:00Commented Apr 20, 2018 at 20:36 -
@PSCL True, but OP was under OS X.Dave Newton– Dave Newton2018年04月20日 20:38:33 +00:00Commented Apr 20, 2018 at 20:38
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
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
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.
Open postgresql.conf, go to the line:
data_directory='/var/lib/postgresql/main/'
is your answer.
-
4This line is commented out in my PostgreSQL.conf.ijt– ijt2021年10月12日 22:55:36 +00:00Commented Oct 12, 2021 at 22:55
-
1In order to 'Open postgresql.conf' to 'go to the line,' one has to already know the answer to the question that OP is asking: "How do I find PostgreSQL's data directory?" Therefore, this answer is not and cannot ever be valid.RussNS– RussNS2025年01月21日 01:03:28 +00:00Commented Jan 21 at 1:03
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;"
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\PostgreSQL9円.4\pg_env.bat
, which contained this line:
@SET PGDATA=E:\POSTGRESQL
That was the answer I needed.
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
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.
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
For M1 Macs with Postgres installed via Homebew, try:
/opt/homebrew/var/[postgresql@12 or other version]
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
-
--bindir
shows the location of the executables, not the data directory.--configure
is OK, even though one has to parse the output a bit.András Váczi– András Váczi2017年10月28日 21:49:46 +00:00Commented 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 ../dataJacques– Jacques2017年10月29日 22:32:20 +00:00Commented Oct 29, 2017 at 22:32
Based on the answer by @Matthew Mark Miller, on macOS 10.14 using Postgres 11 it is located at:
~/Library/Application Support/Postgres
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
on macOS Big Sur version 11.5.2
postgresql 10.17 installed with homebrew
/opt/pgdata
sudo su - postgres
and check the home directory path usingpwd
.SELECT current_setting('data_directory') AS pgdata_path;