官方教程
https://www.postgresql.org/download/linux/redhat/
安装准备
系统版本:CentOS Linux release 7.3.1611 (Core)
软件版本:psql (PostgreSQL) 9.6.17
安装
一、检查PostgreSQL是否已经安装
rpm -qa | grep postgres #检查PostgreSQL 是否已经安装
rpm -qal | grep postgres #检查PostgreSQL 安装位置
如果已经安装,可以使用rpm -e
卸载,卸载之后使用rpm -qa | grep postgres
再次查看是否卸载完成。
二、安装PostgreSQL
-
安装rpm文件
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
-
安装客户端
yum install postgresql96
-
安装服务端
yum install postgresql96-server
-
初始化
/usr/pgsql-9.6/bin/postgresql-96-setup initdb
-
启动PostgreSQL服务并设置开机自启
systemctl start postgresql-9.6 systemctl enable postgresql-9.6
-
查看PostgreSQL版本信息
psql --version
三、配置PostgreSQL
-
修改用户密码
PostgreSQL安装后会创建一个用户,名为postgres。su - postgres #以postgres用户身份操作数据库 psql -U postgres #登录名为postgres的数据库用户 alter user postgres with password '123' #修改数据库用户postgres的密码
-
创建用户和数据库并授权
create user test_user with password 'abc123'; #创建用户 create database test_db owner test_user; #创建数据库 grant all privileges on database test_db to test_user; #授权
-
退出
\q #退出数据库用户 exit #退出postgres系统用户
-
配置远程访问
vi /var/lib/pgsql/9.6/data/postgresql.conf
对所有IP开放远程访问,将localhost
改为*
即可;如果对部分IP开放,多个IP之间用逗号加空格
隔开。 -
信任远程连接
vi /var/lib/pgsql/9.6/data/pg_hba.conf
在IPv4部分添加host all all *.*.*.*/32(需要连接的服务器IP) trust
,如果信任所有IP,IP设为0.0.0.0/0
。
注:最后一项参数:
trust
任何连接都允许,不需要密码
reject
拒绝符合条件(前面几个条件)的请求
MD5
接收一个MD5加密过的密码
password
接收一个密码来登陆,只在可信的网络使用这种方式
gss
使用gssapi认证,只在tcp/ip连接可用
sspi
只在windows可用的一种方式
krb5
不常用,只在TCP/IP可用
ident
使用操作系统用户名认证,验证它是否符合请求的的数据库用户名
ldap
使用LDAP服务器认证
cert
使用ssl客户端认证
pam
使用操作系统的pam模块服务 -
打开防火墙
firewall-cmd --zone=public --list-ports #查看防火墙开放端口列表 firewall-cmd --zone=public --add-port=5432/tcp --permanent #开放5432端口,5432为postgresql端口 firewall-cmd --reload #重启防火墙
常用命令
#启动postgresql
systemctl start postgresql-9.6.service
#停止postgresql
systemctl stop postgresql-9.6.service
#重启postgresql
systemctl restart postgresql-9.6.service
#查看postgresql的状态
systemctl status postgresql-9.6.service
#开机时启用postgresql
systemctl enable postgresql-9.6.service
#开机时关闭postgresql
systemctl disable postgresql-9.6.service
#查看postgresql是否开机启动
systemctl is-enabled postgresql-9.6.service