在postgressql内部修改pg_hba.conf
2020-11-05 08:40 abce 阅读(1634) 评论(0) 编辑 收藏 举报查看pg_hba.conf文件的内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | postgres=# select * from pg_hba_file_rules; line_number | type | database | user_name | address | netmask | auth_method | options | error -------------+-------+---------------+-----------+-----------+-----------------------------------------+-------------+---------+------- 84 | local | { all } | { all } | | | trust | | 86 | host | { all } | { all } | 127.0.0.1 | 255.255.255.255 | trust | | 88 | host | { all } | { all } | ::1 | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust | | 91 | host | { all } | { all } | 27.0.0.0 | 255.0.0.0 | md5 | | 92 | local | {replication} | { all } | | | trust | | 93 | host | {replication} | { all } | 127.0.0.1 | 255.255.255.255 | trust | | 94 | host | {replication} | { all } | ::1 | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust | | 95 | host | { all } | { all } | 27.0.0.0 | 255.0.0.0 | md5 | | (8 rows ) postgres=# |
我们来看看,pg_hba.conf文件在操作系统上位置:
1 2 3 4 5 6 7 | postgres=# select setting from pg_settings where name like '%hba%' ; setting ---------------------------------- /pgdata/11/data/pg_hba.conf (1 row) postgres=# |
知道这些信息后,我们就可以将这些内容加载到表中:
1 2 3 4 5 | postgres=# create table hba(lines text); CREATE TABLE postgres=# copy hba from '/pgdata/11/data/pg_hba.conf' ; COPY 94 postgres=# |
一旦加载到表中,我们就有了pg_hba.conf文件中的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 | postgres=# select * from hba where lines !~ '^#' and lines !~ '^$' ; lines ----------------------------------------------------------------------- local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust host all all 27.0.0.0/8 md5 local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust (7 rows ) postgres=# |
现在我们就可以按照常规表的方式插入记录了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | postgres=# insert into hba (lines) values ( 'host all mydb ::1/128 trust' ); INSERT 0 1 postgres=# select * from hba where lines !~ '^#' and lines !~ '^$' ; lines ----------------------------------------------------------------------- local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust host all all 27.0.0.0/8 md5 local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust host all mydb ::1/128 trust (8 rows ) postgres=# |
再把表的内容写回到文件中:
1 2 3 | postgres=# copy hba to '/pgdata/11/data/pg_hba.conf' ; COPY 95 postgres=# |
读取文件,看看我们新加的规则是否已经在文件中了:
1 2 | postgres=# select pg_read_file( 'pg_hba.conf' ); .... |
修改了配置后,需要reload一下方可生效:
1 2 3 4 5 6 7 | postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t (1 row) postgres=# |
不过,这种方式建议谨慎使用!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2015-11-05 Baseline模板管理
2015-11-05 Baseline管理
2015-11-05 AWR快照管理