Postgres
The Postgres entity runs a PostgreSQL database in a Docker container with automatic image pulling, readiness checking, and SQL initialization.
Import
Section titled “Import”import { Postgres } from "sigil";Constructor
Section titled “Constructor”new Postgres(name: string, config?: Partial<Omit<PostgresConfig, "name">>)Config
Section titled “Config”interface PostgresConfig { name: string // Set via constructor, not config object port?: number // Host port (default: 5432) database?: string // Database name (default: "postgres") username?: string // (default: "postgres") password?: string // (default: "postgres") version?: string // Postgres Docker image tag (default: "16") initSql?: string // Absolute path to SQL file for schema setup seedSql?: string // Absolute path to SQL file for test data}Properties
Section titled “Properties”connectionString
Section titled “connectionString”db.connectionString: stringReturns a standard PostgreSQL connection string:
postgresql://postgres:postgres@localhost:5432/myappAvailable immediately after construction (computed from config values, does not require the database to be running).
pgInterface
Section titled “pgInterface”db.pgInterface: PostgresInterfaceThe TCP interface instance. Exposes the same connection string via getConnectionString().
status
Section titled “status”Inherited from Entity. One of: "pending", "starting", "running", "stopping", "stopped", "error".
Lifecycle
Section titled “Lifecycle”start()
Section titled “start()”- Pulls
postgres:<version>image if not already present - Creates a Docker container named
sigil-<name> - Maps the specified host port to container port 5432
- Sets
POSTGRES_DB,POSTGRES_USER,POSTGRES_PASSWORDenvironment variables - Waits for the database to accept connections (using
psql -c 'SELECT 1'inside the container) - Runs
initSqlfile if provided - Runs
seedSqlfile if provided
stop()
Section titled “stop()”Stops and removes the Docker container.
Examples
Section titled “Examples”Minimal
Section titled “Minimal”const db = env.add(new Postgres("db"));// Postgres 16 on port 5432, database "postgres"Full configuration
Section titled “Full configuration”const db = env.add( new Postgres("shopping-db", { port: 5433, database: "shopping_list", username: "admin", password: "secret", version: "15", initSql: path.join(root, "db/schema.sql"), seedSql: path.join(root, "db/seed.sql"), }));Wiring to a service
Section titled “Wiring to a service”const db = env.add(new Postgres("db", { database: "myapp" }));
env.add( new Service("api", { command: ["node", "server.js"], env: { DATABASE_URL: db.connectionString }, }));