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
feat(postgres): Remove SqlAlchemy dependency from postgres container (testcontainers#445)
Updates the pg testcontainer implementation to not use (and not install)
SQLAlchemy nor psycopg2.
Closes: testcontainers#340Closes: testcontainers#336Closes: testcontainers#320
---------
Co-authored-by: Jason Turim <jason@opscanvas.com>
>>> with PostgresContainer("postgres:latest") as postgres:
49
+
... psql_url = postgres.get_connection_url()
50
+
... engine = sqlalchemy.create_engine(psql_url)
50
51
... with engine.begin() as connection:
51
52
... result = connection.execute(sqlalchemy.text("select version()"))
52
53
... version, = result.fetchone()
53
54
>>> version
54
-
'PostgreSQL 9.5...'
55
+
'PostgreSQL ...'
56
+
57
+
The snippet above will spin up the current latest version of a postgres database in a container. The :code:`get_connection_url()` convenience method returns a :code:`sqlalchemy` compatible url (using the :code:`psycopg2` driver per default) to connect to the database and retrieve the database version.
58
+
59
+
.. doctest::
60
+
61
+
>>> import asyncpg
62
+
>>> from testcontainers.postgres import PostgresContainer
63
+
64
+
>>> with PostgresContainer("postgres:16", driver=None) as postgres:
65
+
... psql_url = container.get_connection_url()
66
+
... with asyncpg.create_pool(dsn=psql_url,server_settings={"jit": "off"}) as pool:
67
+
... conn =await pool.acquire()
68
+
... ret =await conn.fetchval("SELECT 1")
69
+
... assert ret ==1
70
+
71
+
This snippet does the same, however using a specific version and the driver is set to None, to influence the :code:`get_connection_url()` convenience method to not include a driver in the URL (e.g. for compatibility with :code:`psycopg` v3).
72
+
73
+
Note, that the :code:`sqlalchemy` and :code:`psycopg2` packages are no longer a dependency of :code:`testcontainers[postgres]` and not needed to launch the Postgres container. Your project therefore needs to declare a dependency on the used driver and db access methods you use in your code.
55
74
56
-
The snippet above will spin up a postgres database in a container. The :code:`get_connection_url()` convenience method returns a :code:`sqlalchemy` compatible url we use to connect to the database and retrieve the database version.
... result = connection.execute(sqlalchemy.text("select version()"))
19
19
... version, = result.fetchone()
20
20
>>> version
21
-
'PostgreSQL 9.5...'
21
+
'PostgreSQL 16...'
22
22
```
23
23
24
24
The snippet above will spin up a postgres database in a container. The `get_connection_url()` convenience method returns a `sqlalchemy` compatible url we use to connect to the database and retrieve the database version.
0 commit comments