PostgreSQL 设置允许访问IP

PostgreSQL安装后默认只能localhost:5432访问
检验方法:

curl localhost:5432
# 访问成功提示
curl: (52) Empty reply from server
curl 127.0.0.1:5432
# 访问不成功提示
curl: (7) Failed to connect to 172.17.201.227 port 5432: Connection refused

修改pg_hba.conf

pg_hba.confpostgresql.conf的存放目录都在(9.5版本)/etc/postgresql/9.5/main

host  all    all    192.168.1.0/24    trust

表示允许网段192.168.1.0上的所有主机使用所有合法的数据库用户名访问数据库,
其中,数字24是子网掩码,表示允许192.168.1.0–192.168.1.255的计算机访问

修改postgresql.conf

修改listen_addresses=’localhost’, 并放开注释(默认监听localhost)

# 192.168.1.111 为postgresql本机内网地址
listen_addresses='192.168.1.111'

重启postgresql

sudo /etc/init.d/postgresql restart

在本机

curl 192.168.1.111:5432
# 访问成功提示
curl: (52) Empty reply from server

在内网其他机器

curl 192.168.1.111:5432
# 访问成功提示
curl: (52) Empty reply from server

其他 创建用户

进入psql控制台

$ sudo -u postgres -i
$ psql

创建用户 密码

postgres=# CREATE USER myusername WITH PASSWORD 'mypassword' CREATEDB;

创建数据库 用户授权

postgres=# CREATE DATABASE mydb;
postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb to myusername;
postgres=# \q

测试

$ psql -d mydb;
mydb=# \dt

猜你喜欢

转载自blog.csdn.net/wlchn/article/details/78915813