Where does PostgreSQL store the database?

后端 未结 13 1701
忘了有多久
忘了有多久 2020-11-27 09:14

Where are the files for a PostgreSQL database stored?

相关标签:
13条回答
  • 2020-11-27 09:49

    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

    0 讨论(0)
  • 2020-11-27 09:51

    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
    
    0 讨论(0)
  • 2020-11-27 09:56

    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.

    0 讨论(0)
  • 2020-11-27 09:56

    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

    0 讨论(0)
  • 2020-11-27 09:58

    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.

    0 讨论(0)
  • 2020-11-27 09:59

    Under my Linux installation, it's here: /var/lib/postgresql/8.x/

    You can change it with initdb -D "c:/mydb/"

    0 讨论(0)
提交回复
热议问题