一、装数据库
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
firewall-cmd --add-service=postgresql --permanent
firewall-cmd --reload
su - postgres
psql -U postgres
创建用户并设置密码
create user 用户名 with password '密码';
查看用户
\du
修改用户密码
ALTER USER 用户名 with encrypted password '密码';
创建数据库,并指定数据库的拥有者
create database 数据库名 owner 用户名
赋予权限
grant all privileges on database 数据库名 to 用户名;
二、pgsql 远程访问
postgresql安装完成后,不能远程访问,需要修改两个配置文件:
1.vim /var/lib/pgsql/9.6/data/postgresql.conf
listen_addresses = '*' #前面的注释#去掉,并把'localhost'该为 *;
2.vim /var/lib/pgsql/9.6/data/pg_hba.conf
在该配置文件的 host all all 127.0.0.1/32 ident 行下添加以下配置,并注释该行
host all all 0.0.0.0/0 md5
如果限制某些ip的访问,可以将配置0.0.0.0设定为特定的IP值host all all 192.168.56.0/24 md5
host all all 192.168.56.0/32 trust
说明: 允许网段 192.168.56.0 上的所有主机使用所有合法的数据库用户名访问数据库,并提供加密的密码验证。
数字24是子网掩码,表示允许192.168.56.0 到 192.168.56.255的计算机访问
输入systemctl restart postgresql-9.6.service
并回车,重启服务。