简体   繁体   中英

How to connect to postgres in Linode from local system

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 .

  1. In the linode instance, navigate to /var/lib/pgsql/data .

  2. 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
  1. Save the file and restart postgres.
  2. Start SSH tunneling to the linode instance
  3. Change the host and port in the database details to 127.0.0.1 and the port selected during tunneling. In my case it was 5433
  4. Running the python script now should enable the local system to connect with the postgres server running in the linode instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM