PostgreSQL在哪里存储数据库?

时间:2009-07-16 11:55:30

标签: postgresql

PostgreSQL数据库的文件存储在哪里?

13 个答案:

答案 0 :(得分:320)

要查看数据目录的位置,请使用此查询。

show data_directory;

要查看所有运行时参数,请使用

show all;

您可以创建表空间以将数据库对象存储在文件系统的其他部分中。要查看可能不在该数据目录中的表空间,请使用此查询。

SELECT * FROM pg_tablespace;

答案 1 :(得分:56)

在Windows7上,所有数据库都由pg_database下名为C:\Program Files (x86)\PostgreSQL\8.2\data\global的文件中的数字引用。然后,您应该在C:\Program Files (x86)\PostgreSQL\8.2\data\base下按该号码搜索文件夹名称。这是数据库的内容。

答案 2 :(得分:27)

打开pgAdmin并转到特定数据库的“属性”。找到OID,然后打开目录

<POSTGRESQL_DIRECTORY>/data/base/<OID>

应该有你的数据库文件。

答案 3 :(得分:25)

在我的Linux安装下,它位于:/var/lib/postgresql/8.x/

您可以使用initdb -D "c:/mydb/"

进行更改

答案 4 :(得分:23)

正如“PostgreSQL database default location on Linux”中所述,在Linux下,您可以使用以下命令找到:

ps aux | grep postgres | grep -- -D

答案 5 :(得分:14)

特定表/索引的位置可以通过TABLESPACEs调整:

CREATE TABLESPACE dbspace LOCATION '/data/dbs';
CREATE TABLE something (......) TABLESPACE dbspace;
CREATE TABLE otherthing (......) TABLESPACE dbspace;

答案 6 :(得分:11)

在Mac上:/Library/PostgreSQL/9.0/data/base

无法输入目录,但您可以通过以下方式查看内容:sudo du -hc data

答案 7 :(得分:11)

Postgres将数据存储在其数据目录中的文件中。按照以下步骤转到数据库及其文件:

postgresql表文件对应的数据库是一个目录。可以通过运行SHOW data_directory来获取整个数据目录的位置。 在像OS这样的UNIX中(例如:Mac)/Library/PostgreSQL/9.4/data 进入包含所有数据库文件夹的数据目录中的基本文件夹:/Library/PostgreSQL/9.4/data/base

通过运行查找数据库文件夹名称(给出一个整数。这是数据库文件夹名称):

SELECT oid from pg_database WHERE datname = <database_name>;

通过运行找到表文件名(给出一个整数。这是文件名):

SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>; 

这是一个二进制文件。可以像往常一样获取文件详细信息,例如大小和创建日期时间。有关详细信息read this SO thread

答案 8 :(得分:10)

每个人都已经回答了,但只是为了获得最新的更新。如果您想知道所有配置文件所在的位置,请在shell中运行此命令

SELECT name, setting FROM pg_settings WHERE category = 'File Locations';

答案 9 :(得分:8)

我敢打赌你问这个问题是因为你试过pg_ctl start并收到以下错误:

  

pg_ctl:未指定数据库目录且环境变量PGDATA未设置

换句话说,您正在-D命令中查找要放在pg_ctl start之后的目录。

在这种情况下,您要查找的目录包含这些文件。

PG_VERSION      pg_dynshmem     pg_multixact
pg_snapshots    pg_tblspc       postgresql.conf
base            pg_hba.conf     pg_notify   
pg_stat         pg_twophase     postmaster.opts
global          pg_ident.conf   pg_replslot
pg_stat_tmp     pg_xlog         postmaster.pid
pg_clog         pg_logical      pg_serial
pg_subtrans     postgresql.auto.conf    server.log

您可以使用操作系统提供的搜索找到上面的任何文件和目录来找到它。

例如,在我的情况下( HomeBrew 安装在 Mac OS X 上),这些文件位于/usr/local/var/postgres。要启动服务器,请输入:

pg_ctl -D /usr/local/var/postgres -w start

......它有效。

答案 10 :(得分:6)

在Windows上,PostgresSQL docs describe位于C:\Program Files\PostgreSQL\8.1\data之类的PGDATA目录。特定数据库的数据在(例如)C:\Program Files\PostgreSQL\8.1\data\base\100929下,我猜100929是数据库编号。

答案 11 :(得分:0)

朋友的回答是正确的。在Windows上,主要的DB文件夹位置是(至少在我的安装中)

C:\PostgreSQL\9.2\data\base\

,不在程序文件中。

他的2个脚本,将为您提供所需的确切目录/文件:

SELECT oid from pg_database WHERE datname = <database_name>;
SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>; 

我的名字是16393,relfilenode是41603

database files in postgresql

答案 12 :(得分:0)

我正在docker容器(在CentOS上,发生在CentOS上)上运行postgres(9.5),并且正如以上评论中的Skippy le Grand Gourou所述,文件位于/var/lib/postgresql/data/中。

$ docker exec -it my-postgres-db-container bash
root@c7d61efe2a5d:/# cd /var/lib/postgresql/data/
root@c7d61efe2a5d:/var/lib/postgresql/data# ls -lh
total 56K
drwx------. 7 postgres postgres   71 Apr  5  2018 base
drwx------. 2 postgres postgres 4.0K Nov  2 02:42 global
drwx------. 2 postgres postgres   18 Dec 27  2017 pg_clog
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_commit_ts
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_dynshmem
-rw-------. 1 postgres postgres 4.4K Dec 27  2017 pg_hba.conf
-rw-------. 1 postgres postgres 1.6K Dec 27  2017 pg_ident.conf
drwx------. 4 postgres postgres   39 Dec 27  2017 pg_logical
drwx------. 4 postgres postgres   36 Dec 27  2017 pg_multixact
drwx------. 2 postgres postgres   18 Nov  2 02:42 pg_notify
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_replslot
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_serial
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_snapshots
drwx------. 2 postgres postgres    6 Sep 16 21:15 pg_stat
drwx------. 2 postgres postgres   63 Nov  8 02:41 pg_stat_tmp
drwx------. 2 postgres postgres   18 Oct 24  2018 pg_subtrans
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_tblspc
drwx------. 2 postgres postgres    6 Dec 27  2017 pg_twophase
-rw-------. 1 postgres postgres    4 Dec 27  2017 PG_VERSION
drwx------. 3 postgres postgres   92 Dec 20  2018 pg_xlog
-rw-------. 1 postgres postgres   88 Dec 27  2017 postgresql.auto.conf
-rw-------. 1 postgres postgres  21K Dec 27  2017 postgresql.conf
-rw-------. 1 postgres postgres   37 Nov  2 02:42 postmaster.opts
-rw-------. 1 postgres postgres   85 Nov  2 02:42 postmaster.pid
相关问题