I want to configure PostgreSQL to accept connections only from a specified IP. It should not accept requests from any other IP.
Configure PostgreSQL to work for only LOCALHOST or specified ip + port [closed]
Asked Answered
Pg version? OS? Do you mean localhost only? Or "accept connections only from one named non-local IP address" ? –
Bungle
The following example pg_hba.conf allows local and a specified IP to have privileged login, but rejects others.
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host testdb testuser 192.168.1.1/32 md5
host all all 0.0.0.0/0 reject
The easiest way is to make PostgreSQL listen only on localhost
for incoming connections. The relevant parameter is listen_addresses
in postgresql.conf
. The doc is here.
Check the pg_hba.conf file in the data folder of PostgreSQL. This is the client authentication configuration file.
# TYPE DATABASE USER ADDRESS METHOD
host testdb testuser 192.168.1.1 md5
local testdb all md5
Add the above to the pg_hba.conf file
Note that PostgreSQL will still accept TCP socket connections to its port from any interface it is bound to via
listen_addresses
in postgresql.conf
, it just won't let them authenticate. If you want to prevent even a TCP handshake, you'll need to use iptables
. –
Bungle © 2022 - 2024 — McMap. All rights reserved.