我是靠谱客的博主 俊逸万宝路,这篇文章主要介绍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

最后

以上就是俊逸万宝路最近收集整理的关于PostgreSQL 设置允许访问IP的全部内容,更多相关PostgreSQL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(127)

评论列表共有 0 条评论

立即
投稿
返回
顶部