Skip to content

Commit a4b1f34

Browse files
Postgres
1 parent c2ea084 commit a4b1f34

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

docs/modules/postgresql.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# PostgreSQL Module
2-
3-
[PostgreSQL](https://www.postgresql.org/) is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
1+
# PostgreSQL
42

53
## Install
64

@@ -10,33 +8,46 @@ npm install @testcontainers/postgresql --save-dev
108

119
## Examples
1210

11+
These examples use the following libraries:
12+
13+
- [pg](https://www.npmjs.com/package/pg)
14+
15+
npm install pg
16+
npm install @types/pg
17+
18+
Choose an image from the [container registry](https://hub.docker.com/_/postgres) and substitute `IMAGE`.
19+
20+
### Execute a query
21+
1322
<!--codeinclude-->
14-
[Connect and execute query:](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:connect
23+
[](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:pgConnect
1524
<!--/codeinclude-->
1625

26+
### Connect via URI
27+
1728
<!--codeinclude-->
18-
[Connect and execute query using URI:](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:uriConnect
29+
[](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:pgUriConnect
1930
<!--/codeinclude-->
2031

32+
### With database
33+
2134
<!--codeinclude-->
22-
[Set database:](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:setDatabase
35+
[](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:pgSetDatabase
2336
<!--/codeinclude-->
2437

38+
### With username
39+
2540
<!--codeinclude-->
26-
[Set username:](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:setUsername
41+
[](../../packages/modules/postgresql/src/postgresql-container.test.ts) inside_block:pgSetUsername
2742
<!--/codeinclude-->
2843

29-
### Using Snapshots
44+
### Snapshots
3045

31-
This example shows the usage of the postgres module's Snapshot feature to give each test a clean database without having
32-
to recreate the database container on every test or run heavy scripts to clean your database. This makes the individual
33-
tests very modular, since they always run on a brand-new database.
34-
35-
!!! tip
36-
You should never pass the `"postgres"` system database as the container database name if you want to use snapshots.
46+
!!! warning
47+
You should never pass the `"postgres"` system database as the container database name if you want to use snapshots.
3748
The Snapshot logic requires dropping the connected database and using the system database to run commands, which will
3849
not work if the database for the container is set to `"postgres"`.
3950

4051
<!--codeinclude-->
41-
[Test with a reusable Postgres container](../../packages/modules/postgresql/src/postgresql-container-snapshot.test.ts) inside_block:createAndRestoreFromSnapshot
42-
<!--/codeinclude-->
52+
[](../../packages/modules/postgresql/src/postgresql-container-snapshot.test.ts) inside_block:createAndRestoreFromSnapshot
53+
<!--/codeinclude-->

packages/modules/postgresql/src/postgresql-container-snapshot.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { PostgreSqlContainer } from "./postgresql-container";
55
const IMAGE = getImage(__dirname);
66

77
describe("PostgreSqlContainer snapshot and restore", { timeout: 180_000 }, () => {
8-
// createAndRestoreFromSnapshot {
98
it("should create and restore from snapshot", async () => {
9+
// createAndRestoreFromSnapshot {
1010
await using container = await new PostgreSqlContainer(IMAGE).start();
1111

1212
// Connect to the database
@@ -58,8 +58,8 @@ describe("PostgreSqlContainer snapshot and restore", { timeout: 180_000 }, () =>
5858
expect(result.rows[0].name).toEqual("initial data");
5959

6060
await client.end();
61+
// }
6162
});
62-
// }
6363

6464
it("should use custom snapshot name", async () => {
6565
await using container = await new PostgreSqlContainer(IMAGE).start();

packages/modules/postgresql/src/postgresql-container.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { PostgreSqlContainer } from "./postgresql-container";
55
const IMAGE = getImage(__dirname);
66

77
describe("PostgreSqlContainer", { timeout: 180_000 }, () => {
8-
// connect {
98
it("should connect and return a query result", async () => {
9+
// pgConnect {
1010
await using container = await new PostgreSqlContainer(IMAGE).start();
1111

1212
const client = new Client({
@@ -22,28 +22,29 @@ describe("PostgreSqlContainer", { timeout: 180_000 }, () => {
2222
expect(result.rows[0]).toEqual({ "?column?": 1 });
2323

2424
await client.end();
25+
// }
2526
});
26-
// }
2727

28-
// uriConnect {
2928
it("should work with database URI", async () => {
29+
// pgUriConnect {
3030
await using container = await new PostgreSqlContainer(IMAGE).start();
3131

3232
const client = new Client({
3333
connectionString: container.getConnectionUri(),
3434
});
35+
// }
3536
await client.connect();
3637

3738
const result = await client.query("SELECT 1");
3839
expect(result.rows[0]).toEqual({ "?column?": 1 });
3940

4041
await client.end();
4142
});
42-
// }
4343

44-
// setDatabase {
4544
it("should set database", async () => {
45+
// pgSetDatabase {
4646
await using container = await new PostgreSqlContainer(IMAGE).withDatabase("customDatabase").start();
47+
// }
4748

4849
const client = new Client({
4950
host: container.getHost(),
@@ -59,11 +60,11 @@ describe("PostgreSqlContainer", { timeout: 180_000 }, () => {
5960

6061
await client.end();
6162
});
62-
// }
6363

64-
// setUsername {
6564
it("should set username", async () => {
65+
// pgSetUsername {
6666
await using container = await new PostgreSqlContainer(IMAGE).withUsername("customUsername").start();
67+
// }
6768

6869
const client = new Client({
6970
host: container.getHost(),
@@ -79,7 +80,6 @@ describe("PostgreSqlContainer", { timeout: 180_000 }, () => {
7980

8081
await client.end();
8182
});
82-
// }
8383

8484
it("should work with restarted container", async () => {
8585
await using container = await new PostgreSqlContainer(IMAGE).start();

0 commit comments

Comments
 (0)