Where are the files for a PostgreSQL database stored?
Postgres stores data in files in its data directory. Follow the steps below to go to a database and its files:
The database corresponding to a postgresql table file is a directory. The location of the entire data directory can be obtained by running SHOW data_directory
.
in a UNIX like OS (eg: Mac) /Library/PostgreSQL/9.4/data
Go inside the base folder in the data directory which has all the database folders: /Library/PostgreSQL/9.4/data/base
Find the database folder name by running (Gives an integer. This is the database folder name):
SELECT oid from pg_database WHERE datname = <database_name>;
Find the table file name by running (Gives an integer. This is the file name):
SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>;
This is a binary file. File details such as size and creation date time can be obtained as usual. For more info read this SO thread
As suggested in "PostgreSQL database default location on Linux", under Linux you can find out using the following command:
ps aux | grep postgres | grep -- -D
On Windows7 all the databases are referred by a number in the file named pg_database
under C:\Program Files (x86)\PostgreSQL\8.2\data\global
. Then you should search for the folder name by that number under C:\Program Files (x86)\PostgreSQL\8.2\data\base
. That is the content of the database.
On Mac: /Library/PostgreSQL/9.0/data/base
The directory can't be entered, but you can look at the content via: sudo du -hc data
On Windows, the PGDATA directory that the PostgresSQL docs describe is at somewhere like C:\Program Files\PostgreSQL\8.1\data
. The data for a particular database is under (for example) C:\Program Files\PostgreSQL\8.1\data\base\100929
, where I guess 100929 is the database number.
Under my Linux installation, it's here: /var/lib/postgresql/8.x/
You can change it with initdb -D "c:/mydb/"