You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Creating a Postgres contrainer with an async driver fails #263
Hello,
When creating a new database container, testcontainers automatically tries to connect to it here, but if I specify an async driver (e.g. PostgresContainer("postgres:14.4", driver="asyncpg")) sqlalchemy tries to use that and fails because it's not in an async context.
Is there any way to skip trying to connect to the database or fix it so it can also handle async context?
Actually, it's not such a big problem, testcontainers-postgres depends on psycopg2-binary, so you can't get rid of it anyway. And to make get_connection_url return a url with an asynchronous driver, you can just replace the .driver after initializing the container:
Also, a simple solution for #320 would be to just use psycopg2 internally and use driver only to create a connection url, for example, by adding the driver parameter to get_connection_url, (as is done with the host parameter) and passing psycopg2 to it
Also, a simple solution for #320 would be to just use psycopg2 internally and use driver only to create a connection url, for example, by adding the driver parameter to get_connection_url, (as is done with the host parameter) and passing psycopg2 to it
Activity
tillahoffmann commentedon Jan 5, 2023
Hey, do you have a code example to illustrate the issue?
fourteekey commentedon Feb 1, 2023
test_main.py
requirements.txt
For runing use:
pytest -s -k test_simpleandpytest -s -k test_asyncTraceback
You are have discussion in #175 .
For add async drivers need edit core classes.
fourteekey commentedon Mar 13, 2023
@tillahoffmann
Why did you create method
_connectin classDbContainer, with it, you checked the availability of postgres?fourteekey commentedon Mar 13, 2023
MR with fixes #320
kiriharu commentedon Mar 15, 2023
+1 same problem, need this change
axd1x8a commentedon Mar 16, 2023
Actually, it's not such a big problem,
testcontainers-postgresdepends onpsycopg2-binary, so you can't get rid of it anyway. And to makeget_connection_urlreturn a url with an asynchronous driver, you can just replace the.driverafter initializing the container:PS: In my case the url contains
localhostbecause of #108My only concern is, does this session close? Seems to me there's no reason to leave it open.
testcontainers-python/core/testcontainers/core/generic.py
Lines 31 to 35 in fce23a4
axd1x8a commentedon Mar 16, 2023
Also, a simple solution for #320 would be to just use
psycopg2internally and usedriveronly to create a connection url, for example, by adding thedriverparameter toget_connection_url, (as is done with thehostparameter) and passingpsycopg2to ittestcontainers-python/postgres/testcontainers/postgres/__init__.py
Lines 63 to 69 in fce23a4
PookieBuns commentedon May 24, 2023
This functionality could also be extended to mysql as well since mysql currently only supports pymysql as the driver.