使用IP地址限制postgres中的连接数 [英] restrict the number of connections in postgres using an ip address
问题描述
是否可以使用计算机的IP地址来限制postgres中的连接数量,因为我无法找到哪个IP地址被标记为哪个用户/角色。
Is it possible to restrict the number of connections in postgres, using the ip address of machine, since i'm not able to find which ip address is tagged to which user / role.
推荐答案
隐式地可以使用pgbouncer做到-至少1.7版本支持hba文件,因此您可以在ini中绑定db + user + ip并设置db + user的限制文件。这样可以限制与IP或网络的连接。
implicitly you can do it with pgbouncer - at least 1.7 version supports hba file, so you can bond db+user+ip and set limit for db+user in ini file. This way limiting connections to IP or network.
您可以尝试使用HAProxy或仅使用 IPTABLES
(我认为更喜欢
explicitly you can try using HAProxy's or just IPTABLES
(I think prefered way)
最后,您可以编写一些猴子作业,以检查 pg_stat_activity.client_addr
和从pg_stat_activity中选择pg_terminate_backend(pid),其中client_addr ='xxxx'按query_started desc限制10
进行排序(保持前10个连接),但是这种方式对我来说是一个糟糕的选择会说。
lastly you can write some monkey job that will check number of connection from pg_stat_activity.client_addr
and select pg_terminate_backend(pid) from pg_stat_activity where client_addr = 'x.x.x.x' order by query_started desc limit 10
(to keep first 10 connections), but this way is an awful wheel for such task I'd say.
这篇关于使用IP地址限制postgres中的连接数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!