Ubuntu 22.04
Sponsored Link

PostgreSQL 14 : Remote Connection
It's possible to connect to PostgreSQL Server only from Localhost by default like here,
however 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 examaple, Configure scram-sha-256 password method.
vi /etc/postgresql/14/main/postgresql.conf
# line 60 : uncomment and change

listen_addresses = '
vi /etc/postgresql/14/main/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               scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all               scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

# specify network range you allow to connect on [ADDRESS] section
# if allow all, specify []
host    all             all                scram-sha-256

systemctl restart postgresql

[2] To connect to a PostgreSQL Database from remote hosts, set password for each PostgreSQL user.
# connect to own database

psql -d testdb

psql (14.2 (Ubuntu 14.2-1ubuntu1))
Type "help" for help.

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

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

psql -c "alter user ubuntu with password 'password';"

[3] Verify settings to connect to PostgreSQL Database with password from remote hosts.
psql -h www.srv.world -d testdb -U ubuntu

Password for user ubuntu:   # password
psql (14.2 (Ubuntu 14.2-1ubuntu1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

testdb=> # connected
Matched Content