Mehdi Zare
Mehdi Zare

Reputation: 1371

Creating connection pool in psycopg2 using connection string

I created a connection object by passing a connection string to psycopg2.connect and it works fine.

All the settings are saved in a dictionary and called this way:

import psycopg2

connection_string = configs['database']['type'] + "://" + configs['database']['user'] + ":" + configs['database']['pass'] + "@" + configs['database']['server'] + '/' + configs['database']['db']
connection = psycopg2.connect(connection_string)

But I want to create a simple connection pool and passing the same setting generate an error

import psycopg2
from psycopg2 import pool

connection_pool = psycopg2.pool.SimpleConnectionPool(1,100,
                                                     user=configs['database']['user'],
                                                     password=configs['database']['pass'],
                                                     host=configs['database']['type'] + "://" + configs['database']['server'],
                                                     port=5432,
                                                     database=configs['database']['db'])

I get this error message:

psycopg2.OperationalError: could not translate host name "xxxxx.xxxxx.rds.amazonaws.com:5432" to address: Unknown host

I tried different values for host such as adding "postgresql://" at the beginning of the host name but it doesn't work.

Upvotes: 0

Views: 1184

Answers (1)

jjanes
jjanes

Reputation: 44137

It looks like configs['database']['server'] has a ":5432" tacked onto the end. You will either need to change your config to remove it, or parse that data entry into two parts, the real server, and the port number.

Upvotes: 2

Related Questions