I have a python script to add data to a postgres database. During testing, I had created a database in the localsystem and it worked perfectly. Now I have the database running in linode. I am not sure how to connect to the database from the localsytem. I have the database details stored in a python file as follows
DATABASE = 'my_database'
HOST = <IP_ADDRESS> # the ip_address of the linode instance
PORT = '5432'
USER = 'database_user'
PASSWORD = 'database_password'
I use the above code in access the database. in case of the database running in the localhost, the host was equal to HOST = 'localhost'
.
When I run the python script from the localsystem to connect to the database this is the message I get
psycopg2.OperationalError: connection to server at "<IP_ADDESS>", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
I am not sure how to connect to the database running in linode.
I solved this by editing the pg_hba.conf
.
In the linode instance, navigate to /var/lib/pgsql/data
.
In the directory, open the pg_hba.conf
and add the following line under the IPv4 local connections
host all all <local system's Public IP ADDRESS>/32 md5
127.0.0.1
and the port selected during tunneling. In my case it was 5433