How to configure the connection from venv to postgresql?

Viewed 60

I am new to python.

I am creating some programs that do an ETL and save the tables in a postgresql database. The connection to the database is done in the following way.

I have a settings.ini file where I have the variable:

bdd_link = postgresql://postgres:adrian123@localhost:5432/prueba

In a config.py file I have the variable:

from decouple import AutoConfig
link_bdd = config("bdd_link")

And in a sql_con.py file I have the following:

from config import link_bdd
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database

def acceder_bdd():
    url_bdd = link_bdd
    if not database_exists(url_bdd):
        create_database(url_bdd)
        engine = create_engine(url_bdd, echo=False)

This all works when I run it normal.

But when I do it inside a virtual environment (venv), the create_tables() function, which in turn uses the acceder_bdd function, which I use to create tables inside postgresql, gives me the following error:

engine.connect().execute(f"DROP TABLE IF EXISTS {table}")

AttributeError: 'NoneType' object has no attribute 'connect'

It's like the create_engine(url_bdd, echo=False) method isn't working, and it makes me think that database "settings" don't work this way inside a virtual environment.

0 Answers
Related