Registry / testing / testcontainers-postgres

testcontainers-postgres

JSON →
library0.0.1rc1pypypi✓ verified 85d ago

PostgreSQL component of testcontainers-python. Provides a lightweight, disposable PostgreSQL container for integration testing. Current version: 0.0.1rc1, requires Python >=3.7. Release cadence: follows the main testcontainers-python library (major releases every few months).

pip install testcontainers-postgres
INSTALL
IMPORT
SIG · TESTCONTAINERS-POS
T
testcontainers-postgres
testingpythonv0.0.1rc1
Install
4.8s avg
Import
1358ms
Disk
57MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.1rc1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.402s · 56.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.8s · import 1.314s · 58MB
57MB installed
● package 57MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PostgresContainer
from testcontainers.postgres import PostgresContainer
from testcontainers_postgres import PostgresContainer
The package name uses hyphens, but the import path uses dots.

Quickstart: create a PostgreSQL container, get connection URL, and run a query.

from testcontainers.postgres import PostgresContainer with PostgresContainer("postgres:16") as postgres: connection_url = postgres.get_connection_url() print(f"Connection URL: {connection_url}") # Use with psycopg2 or SQLAlchemy import psycopg2 conn = psycopg2.connect(connection_url) cur = conn.cursor() cur.execute("SELECT 1;") print(cur.fetchone())
Debug
Known issues
deprecatedThe `PostgresContainer` constructor argument `db_name` has been renamed to `dbname` in testcontainers-python v4.14.0+.
fix
Use keyword argument `dbname` instead of `db_name`.
affects: >=4.14.0
breakingThe `PostgresContainer` no longer supports the `docker_image` parameter; use the first positional argument (image) instead.
fix
Pass the image as a string directly: `PostgresContainer('postgres:16')` instead of `PostgresContainer(docker_image='postgres:16')`.
affects: >=4.0.0
gotcha`PostgresContainer` silently falls back to default postgres:latest if the provided image tag is not available; this can lead to unexpected version mismatches.
fix
Always specify a full image tag (e.g., 'postgres:16-alpine') to ensure reproducibility.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'testcontainers'
The base package `testcontainers-core` is not installed; testcontainers-postgres only adds the PostgreSQL module.
fix
Install the main package with `pip install testcontainers` or ensure `testcontainers-core` is installed.
ImportError: cannot import name 'PostgresContainer' from 'testcontainers'
The submodule is not imported correctly; the import path uses dots, not underscores.
fix
Use `from testcontainers.postgres import PostgresContainer`.
AttributeError: 'PostgresContainer' object has no attribute 'get_connection_url'
Method name changed in an older version (pre-4.0.0); the correct method is `get_connection_url()`.
fix
Upgrade to latest version and use `get_connection_url()` instead of `get_container_host_ip()` or `get_exposed_port()`.
Upgrade
Version history
0.0.1rc1latest on PyPI · released Jan 6, 2023
Audit
Dependencies
testcontainers-corerequiredRequired base package for container lifecycle management
psycopg2-binaryoptionalRequired for PostgreSQL driver; if not installed, you'll get import errors
sqlalchemyoptionalCommonly used with testcontainers-postgres, but not required
Agent activity
21 hits · last 30 days
node
16
Meta
2
OpenAI (training)
2
Amazon
1
Resources
testcontainers-postgres — pip install testcontainers-postgres · libregistry