Sunday, November 23, 2025

Database-2.Remote Connection

 

PostgreSQL 16 : Remote Connection2023/12/07

 
It's possible to connect to PostgreSQL Server only from Localhost by default like here,
but if you'd like to connect to PostgreSQL from Remote hosts, change settings like follows.
[1]There are many authentication methods on PostgreSQL, though.
On this example, Configure SCRAM-SHA-256 password method.
[root@www ~]# 
vi /var/lib/pgsql/data/postgresql.conf
# line 60 : uncomment and change
# if listens only IPv4, set '0.0.0.0'

listen_addresses = '
*
'
[root@www ~]# 
vi /var/lib/pgsql/data/pg_hba.conf
# add to the end

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
# specify network range you allow to connect on [ADDRESS] section
# if allow all, specify [0.0.0.0/0]
host    all             all             10.0.0.0/24             scram-sha-256

[root@www ~]# 
systemctl restart postgresql

[2]If Firewalld is running, allow PostgreSQL service.
[root@www ~]# 
firewall-cmd --add-service=postgresql

success
[root@www ~]# 
firewall-cmd --runtime-to-permanent

success
[3]To connect to a PostgreSQL Database with password, set password for each PostgreSQL user.
# connect to own database

[cent@www ~]$ 
psql -d testdb

psql (16.0)
Type "help" for help.

# set or change own password
testdb=> \password
Enter new password for user "cent":
Enter it again:
testdb=> \q

# also possible to set or change password for any users with PostgreSQL admin user

[postgres@www ~]$ 
psql -c "alter user cent with password 'password';"

ALTER ROLE
[4]Verify settings to connect to PostgreSQL Database with password from remote hosts.
[root@node01 ~]# 
psql -h www.srv.world -d testdb -U cent

Password for user cent:   # password
psql (16.0)
Type "help" for help.

testdb=> # connected

No comments:

Post a Comment