PostgreSQL服务无法识别initb -D或已更改的data_direectoy

时间:2019-05-17 06:31:21

标签: postgresql centos centos7

我想更改我的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服务器时,终端将无法完成

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的作业失败,因为超过了超时。有关详细信息,请参见“ systemctl状态postgresql.service”和“ journalctl -xe”。
  ✘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-service看不到我已经执行过initdb。当我执行postgresql-setup initdb时,它只是在默认位置下创建数据目录。以postgres用户postgres -D /home2/data的身份运行postgresql确实可以,但是我不得不根据此命令创建某种服务,因此不必保持终端打开。

环境:CentOS 7
我正在本地游民箱中进行首次测试安装。在执行此操作时,我用ansible编写代码。所以通常我不使用root用户;)

1 个答案:

答案 0 :(得分:0)

可能是我问题的解决方案

经过一些研究,我发现了guide,这使我对解决方案有了进一步的了解。看完我的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,就可以将数据库集群安装到正确的目录中,并且可以按照原意使用系统服务。

我将在确认数据库运行正常而没有任何麻烦后将其标记为解决方案。