PostgreSQL database default location on Linux
Solution 1:
The "directory where postgresql will keep all databases" (and configuration) is called "data directory" and corresponds to what PostgreSQL calls (a little confusingly) a "database cluster", which is not related to distributed computing, it just means a group of databases and related objects managed by a PostgreSQL server.
The location of the data directory depends on the distribution. If you install from source, the default is /usr/local/pgsql/data
:
In file system terms, a database cluster will be a single directory under which all data will be stored. We call this the data directory or data area. It is completely up to you where you choose to store your data. There is no default, although locations such as /usr/local/pgsql/data or /var/lib/pgsql/data are popular. (ref)
Besides, an instance of a running PostgreSQL server is associated to one cluster; the location of its data directory can be passed to the server daemon ("postmaster" or "postgres") in the -D
command line option, or by the PGDATA
environment variable (usually in the scope of the running user, typically postgres
). You can usually see the running server with something like this:
[root@server1 ~]# ps auxw | grep postgres | grep -- -D
postgres 1535 0.0 0.1 39768 1584 ? S May17 0:23 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
Note that it is possible, though not very frequent, to run two instances of the same PostgreSQL server (same binaries, different processes) that serve different "clusters" (data directories). Of course, each instance would listen on its own TCP/IP port.
Solution 2:
/var/lib/postgresql/[version]/data/
At least in Gentoo Linux and Ubuntu 14.04 by default.
You can find postgresql.conf
and look at param data_directory
. If it is commented then database directory is the same as this config file directory.
Solution 3:
Connect to a database and execute the command:
SHOW data_directory;
More information:
https://www.postgresql.org/docs/current/sql-show.html https://www.postgresql.org/docs/current/runtime-config-file-locations.html
Solution 4:
Default in Debian 8.1 and PostgreSQL 9.4 after the installation with the package manager apt-get
ps auxw | grep postgres | grep -- -D
postgres 17340 0.0 0.5 226700 21756 ? S 09:50 0:00 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf
so apparently /var/lib/postgresql/9.4/main
.
Solution 5:
On Centos 6.5/PostgreSQL 9.3:
Change the value of "PGDATA=/var/lib/pgsql/data"
to whatever location you want in the initial script file /etc/init.d/postgresql
.
Remember to chmod 700
and chown postgres:postgres
to the new location and you're the boss.