Authentication fails on localhost postgresql with alembic and pytest

I want to connect to a postgresql database via alembic in pytest. I can connect to the database via pg admin with the password i set, but i always get the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "testuser"
</code>
<code>sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "testuser" </code>
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  password authentication failed for user "testuser"

Code is in VisualStudio code and opened in WSL.

docker postgresql db

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>docker run -d --rm --name postgres-container -e POSTGRES_USER=testuser-e POSTGRES_PASSWORD=examplepass -p 5432:5432 postgres:latest
</code>
<code>docker run -d --rm --name postgres-container -e POSTGRES_USER=testuser-e POSTGRES_PASSWORD=examplepass -p 5432:5432 postgres:latest </code>
docker run -d --rm --name postgres-container -e POSTGRES_USER=testuser-e POSTGRES_PASSWORD=examplepass -p 5432:5432 postgres:latest

pytest.ini

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[pytest]
verbosity_assertions = 1
env =
DB_PASS=examplepass
DB_USER=testuser
DB_HOST=localhost
DB_NAME=testdb
DB_PORT=5432
</code>
<code>[pytest] verbosity_assertions = 1 env = DB_PASS=examplepass DB_USER=testuser DB_HOST=localhost DB_NAME=testdb DB_PORT=5432 </code>
[pytest]
verbosity_assertions = 1
env = 
    DB_PASS=examplepass
    DB_USER=testuser
    DB_HOST=localhost
    DB_NAME=testdb
    DB_PORT=5432

conftest.py setup

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@pytest.fixture(scope='module')
def connection_string():
return f'postgresql://{os.getenv("DB_USER")}:{os.getenv("DB_PASS")}@{os.getenv("DB_HOST")}/{os.getenv("DB_NAME")}'
@pytest.fixture(scope='module')
def engine(connection_string):
logging.info(connection_string)
engine = create_engine(connection_string)
logging.info(engine.url)
return engine
@pytest.fixture(scope='module')
def tables(engine):
alembic_cfg = AlembicConfig("alembic.ini")
alembic_cfg.set_main_option('sqlalchemy.url', str(engine.url))
command.upgrade(alembic_cfg, "head")
yield
command.downgrade(alembic_cfg, "base")
@pytest.fixture(scope='module')
def session(engine, tables, context):
"""Returns an sqlalchemy session, and after the test tears down everything properly."""
connection = engine.connect()
Session = sessionmaker(bind=connection)
session = Session()
# inserts example tables
insert_into_tables(session, context)
yield session
</code>
<code>@pytest.fixture(scope='module') def connection_string(): return f'postgresql://{os.getenv("DB_USER")}:{os.getenv("DB_PASS")}@{os.getenv("DB_HOST")}/{os.getenv("DB_NAME")}' @pytest.fixture(scope='module') def engine(connection_string): logging.info(connection_string) engine = create_engine(connection_string) logging.info(engine.url) return engine @pytest.fixture(scope='module') def tables(engine): alembic_cfg = AlembicConfig("alembic.ini") alembic_cfg.set_main_option('sqlalchemy.url', str(engine.url)) command.upgrade(alembic_cfg, "head") yield command.downgrade(alembic_cfg, "base") @pytest.fixture(scope='module') def session(engine, tables, context): """Returns an sqlalchemy session, and after the test tears down everything properly.""" connection = engine.connect() Session = sessionmaker(bind=connection) session = Session() # inserts example tables insert_into_tables(session, context) yield session </code>
@pytest.fixture(scope='module')
def connection_string():
    return f'postgresql://{os.getenv("DB_USER")}:{os.getenv("DB_PASS")}@{os.getenv("DB_HOST")}/{os.getenv("DB_NAME")}'

@pytest.fixture(scope='module')
def engine(connection_string):
    logging.info(connection_string)
    engine = create_engine(connection_string)
    logging.info(engine.url)
    return engine

@pytest.fixture(scope='module')
def tables(engine):
    alembic_cfg = AlembicConfig("alembic.ini")
    alembic_cfg.set_main_option('sqlalchemy.url', str(engine.url))
    command.upgrade(alembic_cfg, "head")
    yield
    command.downgrade(alembic_cfg, "base")

@pytest.fixture(scope='module')
def session(engine, tables, context):
    """Returns an sqlalchemy session, and after the test tears down everything properly."""
    connection = engine.connect()
    Session = sessionmaker(bind=connection)
    session = Session()
    # inserts example tables
    insert_into_tables(session, context)
    yield session

test_file.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sys.path.append(".")
from sqlalchemy import text
def test_db_setup(session):
# Try to execute a simple query to check if a table exists
result_table_exists = session.execute(text("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'task')"))
</code>
<code>sys.path.append(".") from sqlalchemy import text def test_db_setup(session): # Try to execute a simple query to check if a table exists result_table_exists = session.execute(text("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'task')")) </code>
sys.path.append(".")
from sqlalchemy import text

def test_db_setup(session):
    # Try to execute a simple query to check if a table exists
    result_table_exists = session.execute(text("SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'task')"))

logging output shows the correct URL

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>INFO root:conftest.py:47 postgresql://testuser:examplepass@localhost/testdb
INFO root:conftest.py:49 postgresql://testuser:***@localhost/testdb
</code>
<code>INFO root:conftest.py:47 postgresql://testuser:examplepass@localhost/testdb INFO root:conftest.py:49 postgresql://testuser:***@localhost/testdb </code>
INFO    root:conftest.py:47 postgresql://testuser:examplepass@localhost/testdb
INFO    root:conftest.py:49 postgresql://testuser:***@localhost/testdb

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật