PostgreSQL在哪里存储数据库?
PostgreSQL数据库的文件存储在哪里?在我的Linux安装中,它在这里:PostgreSQL在哪里存储数据库?,postgresql,Postgresql,PostgreSQL数据库的文件存储在哪里?在我的Linux安装中,它在这里:/var/lib/PostgreSQL/8.x/ 您可以使用initdb-D“c:/mydb/”对其进行更改特定表/索引的位置可以通过表空间进行调整: CREATE TABLESPACE dbspace LOCATION '/data/dbs'; CREATE TABLE something (......) TABLESPACE dbspace; CREATE TABLE otherthing (......) TA
/var/lib/PostgreSQL/8.x/
您可以使用
initdb-D“c:/mydb/”
对其进行更改特定表/索引的位置可以通过表空间进行调整:
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
CREATE TABLE something (......) TABLESPACE dbspace;
CREATE TABLE otherthing (......) TABLESPACE dbspace;
在Windows上,位于的PGDATA目录类似于
C:\Program Files\PostgreSQL\8.1\data
。特定数据库的数据位于(例如)C:\Program Files\PostgreSQL\8.1\data\base\100929下,我猜100929是数据库号。在Mac上:/Library/PostgreSQL/9.0/data/base
无法输入目录,但您可以通过以下方式查看内容:
sudo du-hc data
要查看数据目录的位置,请使用此查询
show data_directory;
SELECT * FROM pg_tablespace;
要查看所有运行时参数,请使用
show all;
您可以创建表空间以在文件系统的其他部分中存储数据库对象。要查看可能不在该数据目录中的表空间,请使用此查询
show data_directory;
SELECT * FROM pg_tablespace;
在Windows7上,所有数据库都由名为
pg\u database
的文件中的一个数字引用,该文件位于C:\Program Files(x86)\PostgreSQL\8.2\data\global
下。然后,您应该在C:\ProgramFiles(x86)\PostgreSQL\8.2\data\base
下按该编号搜索文件夹名称。这是数据库的内容。打开pgAdmin并转到特定数据库的属性。查找OID,然后打开目录
<POSTGRESQL_DIRECTORY>/data/base/<OID>
/data/base/
应该有您的DB文件。如“”中所建议,在Linux下,您可以使用以下命令查找:
ps aux | grep postgres | grep -- -D
Postgres将数据存储在其数据目录中的文件中。按照以下步骤转到数据库及其文件: 与postgresql表文件相对应的数据库是一个目录。通过运行
SHOW data\u directory
可以获得整个数据目录的位置。
在类UNIX操作系统(例如:Mac)中/Library/PostgreSQL/9.4/data
进入包含所有数据库文件夹的数据目录中的基本文件夹:/Library/PostgreSQL/9.4/data/base
通过运行查找数据库文件夹名称(给出一个整数。这是数据库文件夹名称):
从pg_数据库中选择oid,其中datname=;
通过运行查找表文件名(给出一个整数。这是文件名):
从pg_类中选择relname,relfilenode,其中relname=;
这是一个二进制文件。文件详细信息,如大小和创建日期时间,可以像往常一样获得。有关详细信息我打赌你问这个问题是因为你尝试了
pg_ctl start
并收到以下错误:
pg_ctl:未指定数据库目录,环境变量PGDATA未设置
换句话说,您正在查找在pg_ctl start
命令中放置在-D
之后的目录
在本例中,您要查找的目录包含这些文件
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
您可以通过使用操作系统提供的搜索查找上面的任何文件和目录来找到它
例如,在我的例子中(在Mac OS X上安装自制),这些文件位于/usr/local/var/postgres
中。要启动服务器,请键入:
pg_ctl -D /usr/local/var/postgres -w start
。。。它是有效的。每个人都已经回答了,只是为了最新的更新。如果您想知道所有配置文件的驻留位置,请在shell中运行此命令
SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
picmate的答案是正确的。在windows上,主DB文件夹位置为(至少在我的安装中) 而不是在程序文件中 他的2个脚本将为您提供所需的确切目录/文件:
SELECT oid from pg_database WHERE datname = <database_name>;
SELECT relname, relfilenode FROM pg_class WHERE relname = <table_name>;
从pg_数据库中选择oid,其中datname=;
从pg_类中选择relname,relfilenode,其中relname=;
我的名字是datname 16393和relfilenode 41603
我正在docker容器中运行postgres(9.5)(在CentOS上,恰巧如此),正如上面评论中提到的,文件位于/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
除非问题明确说明了操作系统,否则不要以特定于操作系统的方式回答。为什么不呢?如果你不提及操作系统,只是说“这很有效”,那么任何在不同操作系统上尝试的人都会感到困惑。这是相关的。编辑:哦,你的意思可能是“根本不要给出操作系统特定的答案”。我想这是有道理的,我不知道。@SamGoody,如果这个答案是正确的,你所说的会有一点道理。(这并不完全是错误的,因为文件夹可能在Windows中,但肯定不是给定的)。根据已经给出的答案,可以很容易地发现是否是这样。@senthilkumari我有postgresql 11和windows 10。正如您所提到的,我试图查看全局文件夹中的pg_数据库,但没有看到任何数据库。我可以看到一堆_vm、_fsm、config_exec_参数、pg_控件、pg_filenode.map、pg_internal.init。对于windows 10,我应该搜索名为的文件。是否不同?显示数据\u目录;命令指向数据的确切位置。搜索特定文件夹很痛苦,因为其他人可能已经为您安装了它,而现在您不知道该配置,因此以下sql有助于节省时间:)谢谢Mike,上面写着“必须是超级用户才能检查数据目录”:(如果你不是DBA,你根本不需要知道。顺便说一句-如果有人像我一样在mac上寻找Postgres.app的数据库位置,默认情况下它在~/Library/Application Support/Postgres[ver]/var中。要运行查询,请使用PGAdmin III并使用“运行查询”图标。注意:如果要执行文件系统级备份,请不要只备份这些目录,因为,如下所述:“…您可能会试图从其各自的文件或目录中仅备份或还原某些单独的表或数据库。这将不起作用,因为如果没有包含
$ 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