网上的教程多多少少都有遗漏,没办法,只好自己整理一篇教程

1. 卸载旧版本

# 检查安装的版本
rpm -qa | grep postgres

# 卸载上面打印出的结果 (有依赖关系的,要按顺序卸载)
rpm -e postgresql_xxx_...
...
...

获取yum源

先到官网根据配置获取正确的链接:https://www.postgresql.org/download/linux/redhat/

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安装

yum install postgresql10
yum install postgresql10-server

初始化、开启服务

/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl enable postgresql-10
systemctl start postgresql-10

修改默认用户密码,创建新用户

切换到Linux用户postgres

su - postgres
-bash-4.2$ psql

修改postgres密码

postgres=# \password postgres
# Enter new password:
# Enter it again:
# postgres=# \q

创建新用户和数据库

例:用户名:dbuser 密码:123456 数据库名:userdb

su – postgres
createuser dbuser
createdb userdb
psql
postgres=# alter user dbuser with encrypted password '123456';
# ALTER ROLE 
postgres=# grant all privileges on database mydb to dbuser;
# GRANT

配置PostgreSQL-MD5认证

vi /var/lib/pgsql/10/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only 
local   all             all                                     peer 或 trust
# IPv4 local connections:
#host    all             all             127.0.0.1/32            ident
host    all             all             0.0.0.0/0               password
# IPv6 local connections:
#host    all             all             ::1/128                 ident
host    all             all             ::1/128                 password
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     all                                     peer
#host    replication     all             127.0.0.1/32            ident
#host    replication     all             ::1/128                 ident

配置远程访问

vi /var/lib/pgsql/10/data/postgresql.conf

找到以下两行,去掉注释,改成如下配置

listen_addresses = '*'
port = 5432

关闭firewall

systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state
# not running (关闭成功)

重启数据库

systemctl restart postgresql-10.service