CentOS7安装并配置PostgreSQL

CentOS7安装并配置PostgreSQL

步骤总结

yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install -y postgresql96-server postgresql96-contrib
/usr/pgsql-9.6/bin/postgresql96-setup initdb systemctl start postgresql-9.6 systemctl enable postgresql-9.6
firewall
-cmd --add-service=postgresql --permanent firewall-cmd --reload
su
- postgres psql -U postgres ALTER USER postgres with encrypted password 'abc123'; q exit
vi
/var/lib/pgsql/9.6/data/postgresql.conf vi /var/lib/pgsql/9.6/data/pg_hba.conf systemctl restart postgresql-9.6.service

步骤详情

安装yum源

打开https://yum.postgresql.org/repopackages.php ,找到自己需要的版本。 

以root模式进入CentOS7,输入yum install后面加上刚刚复制的链接,回车。 

yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

安装PostgreSQL

yum install -y postgresql96-server postgresql96-contrib
// 如果使用其他版本的PostgreSQL则需要把其中的两个96换成对应的数字

输入/usr/pgsql-9.6/bin/postgresql96-setup initdb并回车,初始化数据库。 
(如果使用其他版本的PostgreSQL则需要把其中的9.696换成对应的数字) 

输入systemctl start postgresql-9.6并回车,启动服务。 
输入systemctl enable postgresql-9.6并回车,设为开机自启。 
(如果使用其他版本的PostgreSQL则需要把其中的两个9.6换成对应的版本) 

(如果未安装firewalld防火墙可跳过下面两步) 
输入firewall-cmd --add-service=postgresql --permanent并回车,开放防火墙。 
输入firewall-cmd --reload并回车,重启防火墙。

修改默认PostgreSQL用户密码
PostgreSQL安装后会创建一个用户,名为postgres。
输入su - postgres并回车,切换至用户。
输入psql -U postgres并回车,登录数据库。
输入ALTER USER postgres with encrypted password 'abc123';(不要漏了“;”)并回车,设置默认用户postgre的密码,此处密码为abc123,可自行修改。
输入q并回车, 退出数据库。
输入exit并回车,退出用户。

配置远程访问

输入vi /var/lib/pgsql/9.6/data/postgresql.conf并回车。 
(如果使用其他版本的PostgreSQL则需要把其中的9.6换成对应的版本) 
光标下翻,找到listen_addresses。 

按i键进入插入编辑模式,如果想对所有IP开放,则将localhost改为*即可,如果想仅对部分IP开放,多个IP之间用,(逗号+空格)隔开。 
改完之后去掉“listen_address”前面的#。 

编辑完成后,按Esc键,输入:wq并回车。 
输入vi /var/lib/pgsql/9.6/data/pg_hba.conf并回车,将光标移至底部。 
(如果使用其他版本的PostgreSQL则需要把其中的9.6换成对应的版本) 

按i键进入插入编辑模式,在IPv4 local connections下方添加允许连接的IP。 
如果想允许所有IPv4地址,则加入一行host all all 0.0.0.0/0 md5。IPv6方法类似。 

编辑完成后,按Esc键,输入:wq并回车。 
输入systemctl restart postgresql-9.6.service并回车,重启服务。 
(如果使用其他版本的PostgreSQL则需要把其中的9.6换成对应的版本)

重要!!!

firewall-cmd --zone=public --list-ports

firewall-cmd --zone=public --add-port=5432/tcp --permanent          // 5432为postgresql端口

firewall-cmd --reload

原文地址:https://www.cnblogs.com/Paul-watermelon/p/10654303.html