Remote Postgresql Server Connection

I am trying to configure postgresql 9.1 server on ubuntu for remote data access. I installed postgres correctly, the server process is running, and I'm trying to configure it so that I can remotely access the server via the Internet from several other computers outside the local network.

I already changed my pg_hba.conf with

host all all 0.0.0.0 trust 

and postgresql.conf with:

 listen_addresses = '*' port = 5432 

I also modified my iptables to accept connections on port 5432.

When I try to connect using psycopg2 in python:

 conn=psycopg2.connect('host=XX.XX.XX.XX port=5432 dbname=postgres user=myUser password=mypassword') 

I get the following error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect connection_factory=connection_factory, async=async) psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "XX.XX.XX.XX" and accepting TCP/IP connections on port 5432? 

I'm not sure if I inserted the correct IP address, and I am curious to know how exactly I will use the IP address to connect to my server. I used the public IP address of my computer running the postgres server, but I'm not sure if this is correct. Or is there another step I'm still missing? I know this is a bad security style, but for now I would just like to establish a connection. My computer is also behind the router, so how can I access my server?

Any help is greatly appreciated.

+4
source share
1 answer

Your pg_hba.conf should NOT use trust !!! trust means no password is required and I don’t think you want.

This is the correct configuration.

 host all all 0.0.0.0/0 md5 

Pay attention to /0 for 0.0.0.0 .

The full pg_hba.conf should be as follows: -

 local all all trust # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 host all all 0.0.0.0/0 md5 

Note that trust is only applicable for local connections. that is, for applications running on the local IP address 127.0.0.1 on the computer that also starts your postgresql server.

+7
source

Source: https://habr.com/ru/post/1447748/


All Articles