Configure PostgreSQL to work for only LOCALHOST or specified ip + port [closed]
Asked Answered
B

3

30

I want to configure PostgreSQL to accept connections only from a specified IP. It should not accept requests from any other IP.

Beauregard answered 1/8, 2012 at 5:54 Comment(1)
Pg version? OS? Do you mean localhost only? Or "accept connections only from one named non-local IP address" ?Bungle
B
35

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 
Brackely answered 1/8, 2012 at 7:5 Comment(0)
G
18

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.

Glairy answered 1/8, 2012 at 6:43 Comment(0)
M
7

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

Monopteros answered 1/8, 2012 at 6:6 Comment(1)
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.