Bootstrap FreeKB - Postgres (SQL) - Allow Remote Connections
Postgres (SQL) - Allow Remote Connections

Updated:   |  Postgres (SQL) articles

By default, Postgres is configured to only allow connections from localhost. To allow remote connections, update /var/lib/pgsql/data/postgresql.conf with the following.

listen_addresses = '*'

 

And update /var/lib/pgsql/data/pg_hba.conf with the following to allow connections from any IPv4 or IPv6. MD5 is used for password authentication.

# TYPE  DATABASE        USER            ADDRESS              METHOD
host    all             all             0.0.0.0/0            md5
host    all             all             ::/0                 md5

 

Restart Postgres for this change to take effect.

sudo systemctl restart postgresql

 

The psql command with a SQL select pg_settings statement can be used to list the Postgres port. By default, the Postgres port is 5432.

psql --username johndoe --dbname mydb --expanded --command "select * from pg_settings where name = 'port'"
-[ RECORD 1 ]----------------------------------------------------
name       | port
setting    | 5432
unit       |
category   | Connections and Authentication / Connection Settings
short_desc | Sets the TCP port the server listens on.
extra_desc |
context    | postmaster
vartype    | integer
source     | command line
min_val    | 1
max_val    | 65535
enumvals   |
boot_val   | 5432
reset_val  | 5432
sourcefile |
sourceline |

 

If running Postgres on an Amazon Web Services (AWS) EC2 instance, ensure there is an Inbound Security Rule to allow connections on the Postgres Port. Or, you can list the Security Rules using the AWS CLI.

 

The openssl s_client -connect command can be used to check if you are able to connect to the system running Postgres.

~]$ openssl s_client -connect 172.0.0.10:5432
CONNECTED(00000003)
007E13F1A27F0000:error:0A000126:SSL routines:ssl3_read_n:unexpected eof while reading:ssl/record/rec_layer_s3.c:321:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 302 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6dc4ae in the box below so that we can be sure you are a human.