首先进入 PostgreSQL 官网,选择当前系统及版本,就能得到下面第一步中的安装语句。其实命令和之前的差别不大,主要是使用了 dnf ,所以选择对应的版本后就可以愉快的开始安装了。
一、安装 PostgreSQL 流程
# 1、Install the repository RPM:
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 2、Disable the built-in PostgreSQL module(禁用内置的 PostgreSQL模块):
sudo dnf -qy module disable postgresql
# 3、Install PostgreSQL:
sudo dnf install -y postgresql10-server
# 4、Install contrib
yum install postgresql10-contrib -y
# 5、Optionally initialize the database and enable automatic start(初始化数据库和自动启动):
sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
sudo systemctl enable postgresql-10 (开机启动)
sudo systemctl start postgresql-10
二、创建用户和数据库
# 1、使用postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)
su - postgres
# 2、修改密码(可选项)
ALTER USER postgres WITH PASSWORD 'postgres';
# 3、登录postgresql数据库
psql
# 4、创建用户和数据库并授权
create user station with password '*****************'; // 创建用户 station
create database station_20201118 owner station; // 创建数据库 station_20201118 所有者为 station
grant all privileges on database station_20201118 to station; // 授权数据库 station_20201118 给 station
# 5、安装 uuid 扩展支持(查看数据库 \l ,切换数据库 \c )
CREATE EXTENSION pgcrypto;
CREATE EXTENSION "uuid-ossp";
# 6、退出 psql 命令行
\q
三、开启远程访问
# 1、修改 /var/lib/pgsql/10/data/postgresql.conf 文件,
1) 取消 listen_addresses 的注释,将参数值改为 "0.0.0.0"
2) 修改 max_connections 为 500
3) 修改请求日志路径 /data/postgresql/logs/
4) 修改日志文件名为 postgresql-%Y-%m-%d.log
5) 开启自动创建日志文件 log_file_mode = 0600
6) 可选项,修改日志最小类型为 info (#log_min_messages)
7) 确认时区 (log_timezone)
# 2、修改 /var/lib/pgsql/10/data/pg_hba.conf 文件,增加下内容
host all all 0.0.0.0/0 md5
# 3、切换到root用户
su - root
# 4、将文件夹访问权限赋值给 postgres 用户
chown -R postgres.postgres /data/postgresql/logs/
# 5、重启 postgresql 服务
systemctl restart postgresql-10.service
# 6、查看运行状态
systemctl status postgresql-10.service
四、通过防火墙开放外网端口(一般来说不建议将数据库暴露到公网上)
1)通过防火墙控制端口是否放行
# 1、新增允许通过防火墙的端口
firewall-cmd —add-port=5432/tcp —permanent
# 2、删除
firewall-cmd —remove-port=5432/tcp —permanent
# 3、重新加载配置文件
firewall-cmd --reload
2)如果是云服务(例如阿里云)默认防火墙是关闭的,可以通过安全组来允许或拒绝指定端口放行。
五、启动异常及解决方案
启动时失败使用提示中的命令查看错误细节
[root@iZ2vc1kz9v8wfcZ logs]# systemctl start postgresql-10.service
Job for postgresql-10.service failed because the control process exited with error code.
See "systemctl status postgresql-10.service" and "journalctl -xe" for details.
# 查看错误详细命令
journalctl -xe
错误信息显示如下
-- Support: https://access.redhat.com/support
--
-- Unit postgresql-10.service has failed.
--
-- The result is failed.
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ systemd[1]: Starting PostgreSQL 10 database server...
-- Subject: Unit postgresql-10.service has begun start-up
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- Unit postgresql-10.service has begun starting up.
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ postmaster[58481]: 2020-11-18 16:35:12.682 CST [58481] LOG: listening on IPv4 address "0.0.0.0", port 5432
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ postmaster[58481]: 2020-11-18 16:35:12.684 CST [58481] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ postmaster[58481]: 2020-11-18 16:35:12.686 CST [58481] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ postmaster[58481]: 2020-11-18 16:35:12.701 CST [58481] FATAL: could not open log file "/data/postgresql/logs/postgresql-2020-11-18.log": Permission denied
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ postmaster[58481]: 2020-11-18 16:35:12.703 CST [58481] LOG: database system is shut down
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ systemd[1]: postgresql-10.service: Main process exited, code=exited, status=1/FAILURE
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ systemd[1]: postgresql-10.service: Failed with result 'exit-code'.
Nov 18 16:35:12 iZ2vc1kz9v8wfcZ systemd[1]: Failed to start PostgreSQL 10 database server.
-- Subject: Unit postgresql-10.service has failed
原因也说明了,文件/文件夹没有访问权限,解决办法赋值访问权限即可,也就是第三节中第4步的操作。