添加官方源
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
安装PostgreSQL 9.6
#postgresql96-server 数据库核心服务端
#postgresql96-contrib 附加第三方扩展
#postgresql96-devel C语言开发Header头文件和库
yum install postgresql96-server postgresql96-contrib postgresql96-devel
检查是否安装上
rpm -aq| grep postgres
配置文件在哪?
postgresql-9.6.service
#find找一下
find / -type f -name "postgresql-9.6.service"
# yum安装的可能会在这里
vim /usr/lib/systemd/system/postgresql-9.6.service
···
# Location of database directory
Environment=PGDATA=/home/postgresql #你的数据存储目录配置,将会影响之后的权限设置,以及数据库的开启,当然,你可以使用默认的
···
检查是否安装上
[root@pa2 ~]# rpm -aq| grep postgres
postgresql96-libs-9.6.8-1PGDG.rhel7.x86_64
postgresql-libs-9.2.23-3.el7_4.x86_64
postgresql96-server-9.6.8-1PGDG.rhel7.x86_64
postgresql-server-9.2.23-3.el7_4.x86_64
postgresql96-9.6.8-1PGDG.rhel7.x86_64
postgresql96-devel-9.6.8-1PGDG.rhel7.x86_64
postgresql-9.2.23-3.el7_4.x86_64
postgresql96-contrib-9.6.8-1PGDG.rhel7.x86_64
postgresql开启前的准备工作
#创建一个目录,该目录就是你要储存数据的目录
[root@pa2 ~]# mkdir /home/postgresql
#给个权限
[root@pa2 ~]# chown -R postgres:postgres /home/postgresql
[root@pa2 ~]# chmod 700 /home/postgresql
初始化
[root@pa2 ~]# /usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK
开启命令
[root@pa2 ~]# service postgresql-9.6 start #centos7以下,我的是centos7,所以会报错,你懂得
Redirecting to /bin/systemctl start postgresql-9.6.service
[root@pa2 ~]# systemctl start postgresql-9.6.service #centos7用这个开启
加入开机自启
[root@pa2 ~]# systemctl enable postgresql-9.6.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service to /usr/lib/systemd/system/postgresql-9.6.service.
检查下端口
[root@pa2 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3951/java
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22565/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1800/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 10157/postmaster #在这里
修改密码步骤
su postgres #切换postgres
psql #进入数据库
ALTER USER postgres WITH PASSWORD '密码'; #修改用户postgres的密码,必须以分号结束,成功执行后会出现ALTER ROLE
\q #退出,当然你也可以Ctrl + d
su root #返回root用户
连接用户(使用用户)配置
vim pg_hba.conf
···
"local" is for Unix domain socket connections only # 翻译:“本地”仅适用于Unix域套接字连接
local all postgres trust
···
systemctl start postgresql-9.6.service #重启
远程访问配置
要想远程访问,需要修改两处配置,需要重启postgresql
1.
vim postgresql.conf # 该文件作用和 mysql数据库里面的 /etc/my.cnf类似
listen_addresses = '*'
2.
vim pg_hba.conf
host all all 0.0.0.0/0 md5 #代表任何一个ip都能连接
#注意:这个配置是我测试用的,线上生产用请谨慎配置
systemctl start postgresql-9.6.service #重启
信任指定服务器连接
#pg_hba.conf ip段配置示范
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 10.211.55.6/32(需要连接的服务器IP) md5
简单使用
psql -U postgres postgres #连接数据库
# 说明:-h表示主机(Host),-p表示端口(Port),-U表示用户(User)
\l #显示所有数据库: