Centos7安装postgresql9.6
第一步:下载库
以root模式进入CentOS7,输入
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
第六步:修改默认密码
输入 su - postgres并回车,切换至用户。
输入 psql -U postgres并回车,登录数据库。
输入 ALTER USER postgres with encrypted password 'postgres';(不要漏了“;”)并回车
输入 \q并回车, 退出数据库。
输入 exit并回车,退出用户。
第七步:配置远程访问
1.修改postgresql.conf文件
输入
vi /var/lib/pgsql/9.6/data/postgresql.conf
找到listen_addresses ,将该条解释,并且修改为 * 即可 (输入i进入编辑)
编辑完成后,按Esc键,输入:wq并回车。
2. 修改pg_hba.conf文件
输入
vi /var/lib/pgsql/9.6/data/pg_hba.conf
并回车,将光标移至底部。
按i键进入插入编辑模式,在IPv4 local connections下方添加允许连接的IP。
如果想允许所有IPv4地址,则加入一行host all all 0.0.0.0/0 md5。
编辑完成后,按Esc键,输入:wq并回车。
第八步:重启服务
输入
systemctl restart postgresql-9.6.service
并回车
注: 可以在外网测试连接postgres数据库,如果连接上就不用配置防火墙,如果不可以的话请执行下面两步。
1.输入firewall-cmd --add-service=postgresql --permanent并回车,开放防火墙。
2.输入firewall-cmd --reload并回车,重启防火墙。