Skip to content

Commit 6c122d5

Browse files
committed
Update README.rst
Note that Postgres container no longer depends on SqlAlchemy Remove reference to unsupported version of postgres Show an example of using the `driver` parameter
1 parent 4fc30f8 commit 6c122d5

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

README.rst

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,30 @@ Getting Started
4343
>>> from testcontainers.postgres import PostgresContainer
4444
>>> import sqlalchemy
4545

46-
>>> with PostgresContainer("postgres:9.5") as postgres:
47-
... engine = sqlalchemy.create_engine(postgres.get_connection_url())
46+
>>> with PostgresContainer("postgres:latest") as postgres:
47+
... psql_url = postgres.get_connection_url() # postgresql+psycopg2://test:test@localhost:61472/test
48+
... engine = sqlalchemy.create_engine(psql_url)
4849
... result = engine.execute("select version()")
4950
... version, = result.fetchone()
5051
>>> version
51-
'PostgreSQL 9.5...'
52+
'PostgreSQL ......'
53+
54+
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.
55+
56+
.. doctest::
57+
58+
>>> import asyncpg
59+
>>> from testcontainers.postgres import PostgresContainer
60+
61+
>>> with PostgresContainer("postgres:latest", driver=None) as postgres:
62+
... psql_url = container.get_connection_url() # postgresql://test:test@localhost:61472/test
63+
... with asyncpg.create_pool(dsn=psql_url,server_settings={"jit": "off"}) as pool:
64+
... conn = await pool.acquire()
65+
... ret = await conn.fetchval("SELECT 1")
66+
... assert ret == 1
67+
68+
This snippet does the same, however the driver is set to None, to influence the :code:`get_connection_url()` convenience method. Note, that the :code:`sqlalchemy` package is no longer a dependency to launch the Postgres container, so your project must provide support for the specified driver.
5269

53-
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.
5470

5571
Installation
5672
------------

0 commit comments

Comments
 (0)