SoFunction
Updated on 2025-04-08

PostgreSQL Settings operations that allow access to IP

PostgreSQL can only access localhost:5432 by default after installation

Inspection method:

curl localhost:5432
#Access Success Tipscurl: (52) Empty reply from server
curl 127.0.0.1:5432
# Access unsuccessful promptcurl: (7) Failed to connect to 172.17.201.227 port 5432: Connection refused

Modify pg_hba.conf

pg_hba.conf and the storage directory are both in (version 9.5)/etc/postgresql/9.5/main

host all  all  192.168.1.0/24  trust

It means that all hosts on network segment 192.168.1.0 are allowed to access the database using all legitimate database user names.

Where, the number 24 is the subnet mask, indicating that the computer access of 192.168.1.0–192.168.1.255 is allowed.

Revise

Modify listen_addresses='localhost' and release the comments (default listening to localhost)

# 192.168.1.111 is the postgresql native intranet addresslisten_addresses='192.168.1.111'

Restart postgresql

sudo /etc//postgresql restart

On this machine

curl 192.168.1.111:5432
#Access Success Tipscurl: (52) Empty reply from server

Other machines in the intranet

curl 192.168.1.111:5432
#Access Success Tipscurl: (52) Empty reply from server

Other Create a user

Enter the psql console

$ sudo -u postgres -i
$ psql

Create a user password

postgres=# CREATE USER myusername WITH PASSWORD 'mypassword' CREATEDB;

Create a database User authorization

postgres=# CREATE DATABASE mydb;
postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb to myusername;
postgres=# \q

test

$ psql -d mydb;
mydb=# \dt

Supplement: PostgreSQL database enables IP access function

In the data subfolder of the installation directory of PG.

Check whether the following values ​​are listening for connection requests for all IP addresses, as follows:

listen_addresses = '*'

If so, no modification is required.

2.pg_hda.conf

Add a line at the end, as follows:

host  all     all     0.0.0.0/0   md5

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.