PostgreSQL 在哪里存储数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:18:01  来源:igfitidea点击:

Where does PostgreSQL store the database?

postgresql

提问by Teifion

Where are the files for a PostgreSQL database stored?

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

回答by Mike Sherrill 'Cat Recall'

To see where the data directory is, use this query.

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

show data_directory;

To see all the run-time parameters, use

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

show all;

You can create tablespaces to store database objects in other parts of the filesystem. To see tablespaces, which might not be in that data directory, use this query.

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

SELECT * FROM pg_tablespace;

回答by senthilkumari

On Windows7 all the databases are referred by a number in the file named pg_databaseunder 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.

Windows7的上所有的数据库由指定的文件中的一些被称为pg_databaseC:\Program Files (x86)\PostgreSQL\8.2\data\global。然后,您应该在 下按该编号搜索文件夹名称C:\Program Files (x86)\PostgreSQL\8.2\data\base。那就是数据库的内容。

回答by Almir Saraj?i?

Open pgAdmin and go to Properties for specific database. Find OID and then open directory

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

<POSTGRESQL_DIRECTORY>/data/base/<OID>

There should be your DB files.

应该有你的数据库文件。

回答by Skippy le Grand Gourou

As suggested in "PostgreSQL database default location on Linux", under Linux you can find out using the following command:

正如“ Linux 上的 PostgreSQL 数据库默认位置”中所建议的,在 Linux下您可以使用以下命令找到:

ps aux | grep postgres | grep -- -D

回答by Sadegh

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

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

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

你可以改变它 initdb -D "c:/mydb/"

回答by ymv

The location of specific tables/indexes can be adjusted by TABLESPACEs:

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

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

回答by u557022

Postgres stores data in files in its data directory. Follow the steps below to go to a database and its files:

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

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/dataGo inside the base folder in the data directory which has all the database folders: /Library/PostgreSQL/9.4/data/base

一个 postgresql 表文件对应的数据库是一个目录。可以通过运行获取整个数据目录的位置SHOW data_directory。在类似 UNIX 的操作系统(例如:Mac)中,/Library/PostgreSQL/9.4/data进入包含所有数据库文件夹的数据目录中的基本文件夹:/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

这是一个二进制文件。可以照常获取文件详细信息,例如大小和创建日期时间。有关更多信息,请阅读此 SO 线程

回答by xagg

I'd bet you're asking this question because you've tried pg_ctl startand received the following error:

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

pg_ctl: no database directory specified and environment variable PGDATA unset

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

In other words, you're looking for the directory to put after -Din your pg_ctl startcommand.

换句话说,你以后找的目录放置-D在你的pg_ctl start命令。

In this case, the directory you're looking for contains these files.

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

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

You can locate it by locating any of the files and directories above using the search provided with your OS.

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

For example in my case (a HomeBrewinstall on Mac OS X), these files are located in /usr/local/var/postgres. To start the server I type:

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

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

... and it works.

...它的工作原理。

回答by Saad Saadi

Everyone already answered but just for the latest updates. If you want to know where all the configuration files reside then run this command in the shell

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

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

回答by evgeni

On Mac: /Library/PostgreSQL/9.0/data/base

在 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

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