PostgreSQL 设置 PGDATA 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26013504/
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
PostgreSQL set PGDATA variable
提问by Efe Ariaroo
Server: Ubuntu server 14 lts + PostgreSQL 9.2 I want create cluster database using drbd, but i can't set PGDATA without cluster initialization. I just need say pgsql use data from drbd disk. How i can do it?
服务器:Ubuntu 服务器 14 lts + PostgreSQL 9.2 我想使用 drbd 创建集群数据库,但我无法在没有集群初始化的情况下设置 PGDATA。我只需要说 pgsql 使用来自 drbd 磁盘的数据。我该怎么做?
Example 1:
示例 1:
mkdir /cluster/var/lib/pgsql -p
chown postgres:postgres /cluster/var/lib/pgsql -R
cp -R /var/lib/pgsql /cluster/var/lib/pgsql
edit /etc/init.d/postgresql :
PGDATA=/cluster/var/lib/pgsql/data
...
PGLOG=/cluster/var/lib/pgsql/pgstartup.log
/etc/init.d/postgresql start
in postgresql 8.3 it works, but in 9.2 i can't change pgdata in /etc/init.d/postgresql, i need find another file and set pgdata, but, surprise, it's do nothing.
在 postgresql 8.3 中它可以工作,但在 9.2 中我无法更改 /etc/init.d/postgresql 中的 pgdata,我需要找到另一个文件并设置 pgdata,但是,令人惊讶的是,它什么都不做。
Example 2: PGDATA - Specifies the directory where the database cluster is to be stored; can be overridden using the -D option.
示例 2:PGDATA - 指定要存储数据库集群的目录;可以使用 -D 选项覆盖。
Ok, let's start: --pgdata=directory yeah, it's works! but now we have postgresql-xc and error like "postgresql don't know this user - postgresql". drbd start replicate data from cluster, but postgresql start it too.
好的,让我们开始:--pgdata=directory 是的,它有效!但现在我们有 postgresql-xc 和错误,比如“postgresql 不知道这个用户 - postgresql”。drbd 启动从集群复制数据,但 postgresql 也启动它。
UPD 1:
更新 1:
root: initdb --pgdata=/home/username/dir
~initdb not install~bla-bla-bla~use apt-get install postgres-xc
UPD2:
UPD2:
$: /usr/lib/postgresql/9.3/bin/initdb --pgdata=/whateveryouwant
#now you can run postgresql only one way:
$: /usr/lib/postgresql/9.3/bin/postgres -D /see_up
#then:
LOG: database system was shut down at 2014-09-26 15:56:33 YEKT
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
#aaaaaaaaaaand...nothing. just empty console, ^C stopping postgres
#another SSH connect:
$: ps-ela
S 1000 5995 5217 0 80 0 - 62202 poll_s pts/0 00:00:00 postgres
1 S 1000 5997 5995 0 80 0 - 62202 poll_s ? 00:00:00 postgres
1 S 1000 5998 5995 0 80 0 - 62202 poll_s ? 00:00:00 postgres
1 S 1000 5999 5995 0 80 0 - 62202 poll_s ? 00:00:00 postgres
1 S 1000 6000 5995 0 80 0 - 62415 poll_s ? 00:00:00 postgres
1 S 1000 6001 5995 0 80 0 - 26121 poll_s ? 00:00:00 postgres
#is it ok? because...
$: /etc/init.d/postgresql status
9.3/main (port 5432): down
回答by Daniel Vérité
With Ubuntu, to use a specific data directory for a PostgreSQL instance, you just need to do:
对于 Ubuntu,要为 PostgreSQL 实例使用特定的数据目录,您只需要执行以下操作:
# pg_createcluster --datadir=/path/to/the/directory 9.3 nameofcluster
You don't need or want to fiddle with $PGDATA
anywhere. See pg_createclustermanpage.
您不需要或不想在$PGDATA
任何地方摆弄。请参阅pg_createcluster联机帮助页。
If using drdb
to replicate the data directory in a cluster, keep in mind that the term cluster
in PostgreSQL parlance has a different signification: it's always a cluster of databases, which means all the databases served by a specific PostgreSQL instance with its unique data directory. This differs from a drdb cluster
which is a cluster of machines.
如果drdb
用于在集群中复制数据目录,请记住cluster
PostgreSQL术语中的术语具有不同的含义:它始终是数据库集群,这意味着由具有唯一数据目录的特定 PostgreSQL 实例服务的所有数据库。这与drdb cluster
作为机器集群的a 不同。
The data directory of a PostgreSQL cluster cannot be shared at all, in the sense that only one specific instance of PostgreSQL running on a single machine may write into it, otherwise corruption will ensue.
PostgreSQL 集群的数据目录根本不能共享,从某种意义上说,只有在一台机器上运行的 PostgreSQL 的一个特定实例可以写入其中,否则会导致损坏。
If drdb
is configured to conform with this requirement, it's OK to use it, otherwise not.
You may also question why using drdb
in the first place when PostgreSQL has built-in replication optionssince version 8.3 that are more specialized and better suited to a database than pure disk replication.
如果drdb
配置符合这个要求,就可以使用,否则不能。您可能还会问为什么drdb
首先使用PostgreSQL 自 8.3 版以来具有内置复制选项,这些选项比纯磁盘复制更专业且更适合数据库。
回答by Efe Ariaroo
Place the below in your ~/.bashrc file.
将以下内容放在您的 ~/.bashrc 文件中。
PATH=$PATH:/usr/lib/postgresql/<PostgresVersionNumber>/bin
export PATH
export PGDATA="$HOME/<FolderNameForPostgresDatabases>"
Replace 'PostgresVersionNumber' with your installed Postgres version number.
Replace 'FolderNameForPostgresDatabases' with the name of the folder you want your postgres databases to reside. Make sure the folder is created
将“PostgresVersionNumber”替换为您安装的 Postgres 版本号。
将 'FolderNameForPostgresDatabases' 替换为您希望 postgres 数据库驻留的文件夹的名称。确保文件夹已创建
From a terminal, you have to navigate to ~/FolderNameForPostgresDatabases,
then run:
从终端,您必须导航到 ~/FolderNameForPostgresDatabases,然后运行:
initdb
Do that before trying to run:
在尝试运行之前这样做:
pg_ctl start