Postgres variable key invalid in another service
andremaytorena
PROOP

2 years ago

```DATABASEURL = os.environ['DATABASEURL']
print(DATABASE_URL)

if DATABASEURL: print(DATABASEURL)
DBHOST = DATABASEURL.split("@")[1].split(":")[0]
DBPORT = DATABASEURL.split("@")[1].split(":")[1].split("/")[0]
DBNAME = DATABASEURL.split("@")[1].split(":")[1].split("/")[1]
DBUSER = DATABASEURL.split("://")[1].split(":")[0]
DBPASS = DATABASEURL.split("://")[1].split(":")[1].split("@")[0]

# Initialize connection pool
db_pool = pool.SimpleConnectionPool(
    minconn=1,      # Minimum number of connections in the pool
    maxconn=10,     # Maximum number of connections in the pool
    host=DB_HOST,
    port=DB_PORT,
    dbname=DB_NAME,
    user=DB_USER,
    password=DB_PASS
)

def get_connection():
    """Get a connection from the pool."""
    return db_pool.getconn()

def put_connection(conn):
    """Return a connection to the pool."""
    db_pool.putconn(conn)```

I have a python API in a service, and a postgres database in another, I followed these instructions in the image to add the variable for the database url, but i get the error of key invalid when I try to grab it

5 Replies

andremaytorena
PROOP

2 years ago

3930b8f5-a31f-481b-a979-923bd16c0df9


andremaytorena
PROOP

2 years ago

Ok I just realized I had to input it here, but the variable when printed still is empty

1277718470376558600


andremaytorena
PROOP

2 years ago

```DBHOST = os.environ['PGHOST']
DBPORT = os.environ['PGPORT']
DBNAME = os.environ['PGNAME']
DBUSER = os.environ['PGUSER']
DBPASS = os.environ['PGPASSWORD']

Initialize connection pool

dbpool = pool.SimpleConnectionPool( minconn=1, # Minimum number of connections in the pool maxconn=10, # Maximum number of connections in the pool host=DBHOST,
port=DBPORT, dbname=DBNAME,
user=DBUSER, password=DBPASS
)```
i decided to just add the variables into the service, but I get this error when trying to connect to the postgres db:


andremaytorena
PROOP

2 years ago

```File "/opt/venv/lib/python3.11/site-packages/psycopg2/pool.py", line 63, in _connect

conn = psycopg2.connect(*self._args, **self._kwargs)

       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/opt/venv/lib/python3.11/site-packages/psycopg2/init.py", line 122, in connect

conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

psycopg2.OperationalError: could not translate host name "postgres.railway.internal" to address: Name or service not known```


andremaytorena
PROOP

2 years ago

Ohhh it's because it's in another project, I didn't realize they had to be in the same project


Loading...