This repository contains a Dockerfile for creating a PostgreSQL 17 container with the pgTAP extension pre-installed. pgTAP is a unit testing framework for PostgreSQL that allows you to write tests for your database in a familiar TAP (Test Anything Protocol) format.
- Based on the official PostgreSQL 17 image.
- Includes pgTAP for database unit testing.
- Pre-configured to enable the
pgTAP
extension on container startup. - Ready-to-use container for testing PostgreSQL databases.
pgTAP is a unit testing framework for PostgreSQL that provides a set of functions for writing tests in SQL. It supports functions like ok()
, is()
, and isnt()
to test database objects, queries, and logic.
TAP, the Test Anything Protocol, is a simple text-based interface between testing modules in a test harness. It decouples the reporting of errors from the presentation of the reports.
You can pull the pre-built image from Docker Hub:
docker pull pshaddel/postgres-pgtap:latest
Run the container with the following command:
docker run -d --name my_postgres_pgtap \
-e POSTGRES_USER=myuser \
-e POSTGRES_PASSWORD=mypassword \
-e POSTGRES_DB=mydb \
-p 5432:5432 \
pshaddel/postgres-pgtap:latest
You can access the database in two ways:
-
Using a shell inside the container:
docker exec -it my_postgres_pgtap sh
Then, use
psql
to connect to the database:psql -U myuser -d mydb
-
Using a PostgreSQL client:
Connect to the database using the environment variables and the exposed port:
psql -h localhost -p 5432 -U myuser -d mydb
Once connected to the database, you can verify that the pgTAP
extension is installed by running:
SELECT * FROM pg_available_extensions WHERE name = 'pgtap';
You should see pgTAP
listed as an available extension.
To write tests, you can use pgTAP functions like ok()
, is()
, and isnt()
. For example:
SELECT plan(2);
SELECT ok(1 = 1, '1 equals 1');
SELECT is(2 + 2, 4, '2 plus 2 equals 4');
SELECT * FROM finish();
If you want to build the image locally, clone this repository and run:
docker build -t postgres-pgtap .
Then, run the container as described above.
The Dockerfile performs the following steps:
- Starts with the official PostgreSQL 17 image.
- Installs required dependencies for building and running pgTAP.
- Installs the
TAP::Parser::SourceHandler::pgTAP
Perl module. - Clones, builds, and installs pgTAP from its official repository.
- Enables the
pgTAP
extension by adding it to the PostgreSQL initialization scripts.
If you encounter any issues:
-
Ensure that the required ports (default:
5432
) are not blocked by your firewall. -
Verify that the environment variables (
POSTGRES_USER
,POSTGRES_PASSWORD
,POSTGRES_DB
) are correctly set. -
Check the container logs for errors:
docker logs my_postgres_pgtap
This project is licensed under the MIT License.