环境:CentOS
服务器:192.168.181.129
安装PostgreSQL
1.下载rpm包
#下载rpm包
wget https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2.安装rpm
#安装rpm包
yum install pgdg-redhat-repo-latest.noarch.rpm
#rpm -ivh pgdg-redhat-repo-latest.noarch.rpm
3.安装postgresql96-server
yum install postgresql-server
4.postgresql初始化
#进入目录
cd /usr
#创建data文件夹用于postgresql初始化
mkdir data
#文件夹修改所属人
chown postgres:postgres /usr/data
#查询initdb所在位置
find / -name initdb
#进入目录
cd /usr/bin
#切换postgres
su postgres
#初始化postgresql
initdb -D /usr/data
postgresql-setup initdb
5.配置pg_hub.conf | postgresql.conf
#pg_hub.conf配置
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
#--------------------------------------------->>>添加如下语句
host all all 0.0.0.0/0 trust
#--------------------------------------------->>>
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres trust
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
#postgresql.conf 配置
#--------------->>>添加如下语句
listen_addresses = '*'
port = 5432
password_encryption = on
#--------------->>>以下是PostgreSQL默认配置
max_connections = 100
shared_buffers = 32MB
log_timezone = 'PRC'
lc_messages = 'zh_CN.UTF-8'
lc_monetary = 'zh_CN.UTF-8'
lc_numeric = 'zh_CN.UTF-8'
lc_time = 'zh_CN.UTF-8'
6.启动postgresql
#启动postgresql
pg_ctl -D /usr/data/ -l logfile start
#停止postgresql
pg_ctl -D /usr/data/ -l logfile stop
#重启postgresql
pg_ctl -D /usr/data/ -l logfile restart
7.测试连接(trust)
8.修改postgresql密码
#进入数据库
sudo -u postgres psql
#修改数据库密码
ALTER USER postgres WITH PASSWORD 'password';
9.修改pg_hub.conf
#pg_hub.conf配置
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
#--------------------------------------------->>>修改METHOD为md5
host all all 0.0.0.0/0 md5
#--------------------------------------------->>>
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres trust
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128 trust
10.重启数据库
#切换用户
su postgres
#重启postgresql
pg_ctl -D /usr/data/ -l logfile restart
11.测试连接(md5)
rk