CentOS7安装配置PostgreSQL9.6

本文记录参考网络文档实际安装的全过程,以及安装过程中遇到的问题

一.验证环境

1.操作系统

cat /etc/redhat-release

cat /etc/os-release

CentOS Linux release 7.1.1503 (Core)

2.PostgresSQL版本

PostgreSQL 9.6.3 https://www.postgresql.org/download/linux/RedHat/

二.安装

2.1 安装rpm

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

2.2 安装客户端

yum install -y postgresql96

2.3 安装服务器端

#yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL; #同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成,需要进入数据库后修改; #PostgreSQL在数据库用户同名的系统账号下登录免密。




yum install -y postgresql96-server

2.4 数据库初始化

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

2.5 设置开机启动

systemctl enable postgresql-9.6

2.6 启动数据库服务

systemctl start postgresql-9.6

三.配置使用

3.1 修改用户密码

#yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL;

[root@psql_master ~]# su - postgres

#切换用户后,提示符变更为“-bash-4.2$”;

#同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成;

#PostgreSQL在数据库用户同名的系统账号下登录免密;

-bash-4.2$ psql -U postgres

#进入数据库后修改密码;

postgres=# alter user postgres with password 'postgres';
ALTER ROLE

注意:这里命令后面一定要加“;”号,并且出现"ALTER ROLE"否则执行无效。

  • postgres命令行模退出使用
postgres=#\q
  • 带端口模式登陆命令行客户端
-bash-4.2$ psql -p5432 postgre

3.2 允许远程访问

#配置文件中,默认只能本机访问postgresql;

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

修改listen_addresses = 'localhost'为listen_addresses = '*'

3.3 主机认证

#在第82行之后,”IPv4 local connections”下新增允许全网段的IP访问;

#“host” 代表主机类型,第一个“all”代表db ,第二个“all”代表user ,“172.29.3.67/32” 代表client ip,“trust”代表认证方式;

#认证方式除“trust”外,还有“peer”, “ident”, “md5”,“password”等,具体可参考pg-hba https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html

vi /var/lib/pgsql/9.6/data/pg_hba.conf

# IPv4 local connections:
host    all             all             127.0.0.1/32            ident

#Allow all ip list
host    all             all             0.0.0.0/0             md5

3.4 重启服务

systemctl restart postgresql-9.6

3.5 配置防火墙端口

  
 #配置防火墙端口
 firewall-cmd --zone=public --add-port=5432/tcp --permanent
 #查看已经配置的防火墙端口
 firewall-cmd --zone=public --list-ports
 #重启防火墙
 systemctl restart firewalld.service

四.参考文档

https://www.linuxidc.com/Linux/2017-10/147536.htm

猜你喜欢

转载自my.oschina.net/u/614774/blog/1810233