解决安装PostgreSQL 9.6时遇到的困难和问题

发表时间: 2020-07-19 15:54

yum 安装PostgreSQL

安装

yum install  https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-redhat-repo-42.0-11.noarch.rpmyum install postgresql96

初始化数据库

cd pgsql-9.6//usr/pgsql-9.6/bin/postgresql96-setup initdb

执行完毕显示:

Initializing database ... OK

创建用户组和用户:

创建用户组:

sudo groupadd postgresql

创建用户:

sudo useradd -gpostgresql postgresql

实现开机自启服务

systemctl enable postgresql-9.6Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service to /usr/lib/systemd/system/postgresql-9.6.service.


启动数据库服务

systemctl start  postgresql-9.6

如果发生报错情况:

Job for postgresql-9.6.service failed because the control process exited with error code. See "systemctl status postgresql-9.6.service" and "journalctl -xe" for details.

解决方案1:

systemctl status postgresql-9.6

查看启动状态:

有如下报错:

 postgresql-9.6.service - PostgreSQL 9.6 database server   Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)   Active: failed (Result: exit-code) since Wed 2020-06-10 13:26:43 CST; 26s ago     Docs: https://www.postgresql.org/docs/9.6/static/  Process: 24750 ExecStart=/usr/pgsql-9.6/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)  Process: 24743 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Main PID: 24750 (code=exited, status=1/FAILURE)Jun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 systemd[1]: Starting PostgreSQL 9.6 database server...Jun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 postmaster[24750]: < 2020-06-10 13:26:43.566 CST > LOG:  redirecting log output to logging collector processJun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 postmaster[24750]: < 2020-06-10 13:26:43.566 CST > HINT:  Future log output will appear in directory "pg_log".Jun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 systemd[1]: postgresql-9.6.service: main process exited, code=exited, status=1/FAILUREJun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 systemd[1]: Failed to start PostgreSQL 9.6 database server.Jun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 systemd[1]: Unit postgresql-9.6.service entered failed state.Jun 10 13:26:43 hecs-x-xlarge-2-linux-20200604174817 systemd[1]: postgresql-9.6.service failed.

权限的问题,授权一下:

chmod 700 /var/lib/pgsql/9.6/data

再次启动:正常了。

解决方案2:很有可能是以前安装过PostgreSQL ,那就是需要删除/var/lib/pgsql

rm -rf   /var/lib/pgsql

再次启动:正常了。

查看是否安装成功

[root@hecs-x-xlarge-2-linux-20200604174817 data]# netstat -tlun | grep 5432  #postgres是占用5432端口tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN     tcp6       0      0 :::5432                 :::*                    LISTEN