PostgresDatabase
Workload for PostgreSQL databases.
Description
With this workload, you can define PostgreSQL databases.
A PostgresDatabase workload is initialized with a valid database name.
See examples.
Each workload has an environment variable name associated with it that holds the connection string for the database. The variable is named after the workload's databaseName. For example:
- database name
products:ML_PG_PRODUCTS_DATABASE_URL.
Client
You can use any PostgreSQL database client with the workload.
See examples.
Server
By default, each PostgresDatabase is associated to a different database server.
Development environment
A Docker container for the dev environment is launched with npx monolayer start dev
You can stop it with npx monolayer stop dev.
After the container is started:
- The environment variable with the connection string for the workload's Docker container will be written to
.env.local. - The database will be created on the database server if it does not already exist.
INFO
Check your framework's documentation to see if the .env.local file is loaded automatically.
Test environment
A Docker container for the test environment is launched with npx monolayer start test
You can stop it with npx monolayer stop test.
- The environment variable with the connection string for the workload's Docker container will be written to
.env.test.local. - The database will be created on the database server if it does not already exist.
INFO
Check your framework's documentation to see if the .env.test.local file is loaded automatically.
Examples
Using Workloads in the Same Database Server
import { PostgreSQL } from "@monolayer/sdk";
import pg from "pg";
export const productsDb = new PostgresDatabase("products");
const client = new pg.Pool({
// Assumes the environment variable is set
connectionString: process.env[productsDb.connectionStringEnvVar],
});
client.query("SELECT 1");