initb -d或更改的data_direectoy均未通过PostgreSQL服务识别



我想更改我的postgresql数据库群集的data_directory。我找到了两种方法,其中不为我工作。

从文档中,我的工作是:

yum install postgresql-server
create new linux user "postgres"
sudo mkdir /home2
sudo mkdir /home2/data
sudo chown postgres:postgres /home2
sudo chown postgres:postgres /home2/data

现在,麻烦在两种情况下都开始:

变体1

✘ root@localhost /var/lib/pgsql/data # postgresql-setup initdb
Initializing database ... OK
✘ root@localhost /var/lib/pgsql/data # l
total 44K
drwx------. 15 postgres postgres 4.0K May 17 08:02 .
drwx------.  4 postgres postgres   72 May 16 15:17 ..
drwx------.  5 postgres postgres   41 May 17 08:02 base
drwx------.  2 postgres postgres 4.0K May 17 08:02 global
drwx------.  2 postgres postgres   18 May 17 08:02 pg_clog
-rw-------.  1 postgres postgres 4.2K May 17 08:02 pg_hba.conf
-rw-------.  1 postgres postgres 1.6K May 17 08:02 pg_ident.conf
drwx------.  2 postgres postgres    6 May 17 08:02 pg_log
drwx------.  4 postgres postgres   36 May 17 08:02 pg_multixact
drwx------.  2 postgres postgres   18 May 17 08:02 pg_notify
drwx------.  2 postgres postgres    6 May 17 08:02 pg_serial
drwx------.  2 postgres postgres    6 May 17 08:02 pg_snapshots
drwx------.  2 postgres postgres    6 May 17 08:02 pg_stat_tmp
drwx------.  2 postgres postgres   18 May 17 08:02 pg_subtrans
drwx------.  2 postgres postgres    6 May 17 08:02 pg_tblspc
drwx------.  2 postgres postgres    6 May 17 08:02 pg_twophase
-rw-------.  1 postgres postgres    4 May 17 08:02 PG_VERSION
drwx------.  3 postgres postgres   60 May 17 08:02 pg_xlog
-rw-------.  1 postgres postgres  20K May 17 08:02 postgresql.conf
root@localhost /var/lib/pgsql/data # 

以Postgres-user启动终端:

-bash-4.2$ psql
psql (9.2.24)
Type "help" for help.
postgres=# SHOW data_directory;
   data_directory    
---------------------
 /var/lib/pgsql/data
(1 row)
postgres=#

我做了systemctl stop psotgresql,编辑了postgresql.conf并更改了data_directory = '/home2/data'。当我做systemctl start psotgresql时,我会得到

FATAL:  "/home2/data" is not a valid data directory
DETAIL:  File "/home2/data/PG_VERSION" is missing.

所以我做了

-bash-4.2$ initdb -D /home2/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
fixing permissions on existing directory /home2/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /home2/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
    postgres -D /home2/data
or
    pg_ctl -D /home2/data -l logfile start
-bash-4.2$

作为Postgres用户。当我尝试使用systemctl start postgresql再次启动PostgreSQL Server时,终端将无法完成

root@localhost /var/lib/pgsql/data # systemctl start postgresql

但是服务器正在运行,我可以登录Postgres用户

-bash-4.2$ psql
psql (9.2.24)
Type "help" for help.
postgres=# SHOW data_directory;
 data_directory 
----------------
 /home2/data
(1 row)
postgres=#

这里出了什么问题?服务为什么"提示"完成?一段时间后,服务将暂停并回来。数据库不再运行。

PostgreSql.Service的工作失败,因为超出了超时。有关详细信息 ✘root@localhost/var/lib/pgsql/data#


变体2

新的新鲜本地VM从顶部做了步骤直到麻烦开始:

-bash-4.2$ initdb -D /home2/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
fixing permissions on existing directory /home2/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /home2/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
    postgres -D /home2/data
or
    pg_ctl -D /home2/data -l logfile start
-bash-4.2$ ls -l /home2/data/
total 40
drwx------. 5 postgres postgres    41 May 17 08:20 base
drwx------. 2 postgres postgres  4096 May 17 08:20 global
drwx------. 2 postgres postgres    18 May 17 08:20 pg_clog
-rw-------. 1 postgres postgres  4476 May 17 08:20 pg_hba.conf
-rw-------. 1 postgres postgres  1636 May 17 08:20 pg_ident.conf
drwx------. 4 postgres postgres    36 May 17 08:20 pg_multixact
drwx------. 2 postgres postgres    18 May 17 08:20 pg_notify
drwx------. 2 postgres postgres     6 May 17 08:20 pg_serial
drwx------. 2 postgres postgres     6 May 17 08:20 pg_snapshots
drwx------. 2 postgres postgres     6 May 17 08:20 pg_stat_tmp
drwx------. 2 postgres postgres    18 May 17 08:20 pg_subtrans
drwx------. 2 postgres postgres     6 May 17 08:20 pg_tblspc
drwx------. 2 postgres postgres     6 May 17 08:20 pg_twophase
-rw-------. 1 postgres postgres     4 May 17 08:20 PG_VERSION
drwx------. 3 postgres postgres    60 May 17 08:20 pg_xlog
-rw-------. 1 postgres postgres 19865 May 17 08:20 postgresql.conf
-bash-4.2$ 

尝试启动PostgreSQL服务

✘ root@localhost /var/lib/pgsql/data # systemctl restart postgresql
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
✘ root@localhost /var/lib/pgsql/data # journalctl -xe
...
May 17 08:20:58 localhost.localdomain postgresql-check-db-dir[15283]: "/var/lib/pgsql/data" is missing or empty.
May 17 08:20:58 localhost.localdomain postgresql-check-db-dir[15283]: Use "postgresql-setup initdb" to initialize the database cluster.
May 17 08:20:58 localhost.localdomain postgresql-check-db-dir[15283]: See /usr/share/doc/postgresql-9.2.24/README.rpm-dist for more information.
May 17 08:20:58 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1
May 17 08:20:58 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server.
...

因此,PostgreSQL服务没有看到我已经做了Initdb。当我进行postgresql-setup initdb时,它只会在默认位置创建数据目录。运行PostgreSQL作为Postgres用户postgres -D /home2/data确实有效,但是我必须在此命令中创建某种服务,因此我不必保持终端打开。

环境:Centos 7
我正在当地的流浪汉盒中进行第一次测试安装。在这样做的过程中,我以Ansible编写代码。因此,通常我不使用root用户;(

可能是我问题的解决方案

进行了一些研究后,我找到了一份指南,这给了我进一步的解决方案。查看我的cat /usr/lib/systemd/system/postgresql.service后,有一部分说

# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql.service",
# containing
#   .include /lib/systemd/system/postgresql.service
#   ...make your changes here...

所以我这样做了:

# vi /etc/systemd/system/postgresql.service
.include /lib/systemd/system/postgresql.service
[Service]
Environment=PGDATA=/home2/data

最后,我可以简单地做postgresql-setup initdb,而我的数据库群集已安装到正确的目录中,我可以像本来一样使用我的系统服务。

我一旦可以确认数据库运行良好而不会遇到任何麻烦。

最新更新