PostgreSQL 配置远程访问
环境
- postgresql 14.6
- CentOS Linux release 7.9.2009 (Core)
配置远程访问
默认情况下,PostgreSQL只允许localhost(127.0.0.1)的连接。如果数据库服务器和程序不在一台服务器上,那么将无法远程连接。
开启相应的防火墙端口,允许端口5432
# 添加指定端口
[root@192 ~]# firewall-cmd --add-port=5432/tcp --permanent
success
# 重载入端口
[root@192 ~]# firewall-cmd --reload
success
# 查询端口是否开启成功
[root@192 ~]# firewall-cmd --query-port=5432/tcp
yes
复制
修改配置文件
- 修改默认安装目录(/opt/pgsql/postgresql/data)目录下的两个文件。
- postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
- pg_hba.conf 配置对数据库的访问权限。
[root@192 ~]# su - postgres 上一次登录:五 12月 30 21:27:39 CST 2022pts/1 上 [postgres@192 ~]$ cd /opt/pgsql/postgresql/data [postgres@192 data]$ ll 总用量 72 drwx------. 5 postgres postgres 41 12月 27 22:41 base drwx------. 2 postgres postgres 4096 12月 30 11:33 global drwx------. 2 postgres postgres 6 12月 27 22:41 pg_commit_ts drwx------. 2 postgres postgres 6 12月 27 22:41 pg_dynshmem -rw-------. 1 postgres postgres 4859 12月 27 22:57 pg_hba.conf -rw-------. 1 postgres postgres 1636 12月 27 22:41 pg_ident.conf drwx------. 4 postgres postgres 68 12月 30 20:20 pg_logical drwx------. 4 postgres postgres 36 12月 27 22:41 pg_multixact drwx------. 2 postgres postgres 6 12月 27 22:41 pg_notify drwx------. 2 postgres postgres 6 12月 27 22:41 pg_replslot drwx------. 2 postgres postgres 6 12月 27 22:41 pg_serial drwx------. 2 postgres postgres 6 12月 27 22:41 pg_snapshots drwx------. 2 postgres postgres 6 12月 30 20:15 pg_stat drwx------. 2 postgres postgres 25 12月 30 21:29 pg_stat_tmp drwx------. 2 postgres postgres 18 12月 27 22:41 pg_subtrans drwx------. 2 postgres postgres 6 12月 27 22:41 pg_tblspc drwx------. 2 postgres postgres 6 12月 27 22:41 pg_twophase -rw-------. 1 postgres postgres 3 12月 27 22:41 PG_VERSION drwx------. 3 postgres postgres 60 12月 27 22:41 pg_wal drwx------. 2 postgres postgres 18 12月 27 22:41 pg_xact -rw-------. 1 postgres postgres 88 12月 27 22:41 postgresql.auto.conf -rw-------. 1 postgres postgres 28762 12月 27 22:52 postgresql.conf -rw-------. 1 postgres postgres 69 12月 30 20:15 postmaster.opts -rw-------. 1 postgres postgres 85 12月 30 20:15 postmaster.pid -rw-rw-r--. 1 postgres postgres 5028 12月 30 19:50 serverlog
复制
# 修改数据库配置文件postgresql.conf
[postgres@192 data]$ vi postgresql.conf
# 监听端口注释打开,并修改为*,表示可以任意ip访问
listen_addresses = '*'
复制
# 修改数据库客户端授权配置文件pg_hba.conf
[postgres@192 data]$ vi pg_hba.conf
# 在ipv4链接设置中添加一行(IPv4 local connections)
host all all 0.0.0.0/0 trust
复制
# 修改完配置文件后重启数据库即可
[root@192 ~]# service postgresql restart
Restarting PostgreSQL: ok
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
2325次阅读
2025-04-09 15:33:27
外国CTO也感兴趣的开源数据库项目——openHalo
小满未满、
1123次阅读
2025-04-21 16:58:09
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1074次阅读
2025-04-27 16:53:22
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
666次阅读
2025-04-10 15:35:48
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
537次阅读
2025-04-11 09:38:42
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
513次阅读
2025-04-14 09:40:20
最近我为什么不写评论国产数据库的文章了
白鳝的洞穴
474次阅读
2025-04-07 09:44:54
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
430次阅读
2025-04-17 17:02:24
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
424次阅读
2025-04-30 15:24:06
天津市政府数据库框采结果公布,7家数据库产品入选!
通讯员
406次阅读
2025-04-10 12:32:35