Local Postgres
Run Postgres for development the same way you run your cloud resources: declared in your Stack, created with one deploy, and removed with one destroy. Docker gives you a pinned image, a network the database is reachable on by name, a volume that keeps its data, and the container itself.
You’ll need a Docker engine and the provider installed, as in Setup. The full program is the checked-in examples/docker-postgres.
- image
- postgres:18-alpine
Pin the image
Section titled “Pin the image”import * as Alchemy from "alchemy";import * as Docker from "@alchemy.run/docker";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";
export default Alchemy.Stack( "DockerPostgresExample", { providers: Layer.merge(Docker.providers(), Alchemy.RandomProvider()), state: Alchemy.localState(), }, Effect.gen(function* () { const image = yield* Docker.RemoteImage("postgres-image", { name: "postgres", tag: "18-alpine", alwaysPull: false, }); // ... }),);RemoteImage pulls postgres:18-alpine, and alwaysPull: false keeps the copy you have instead of pulling again on every deploy. Alchemy.localState() keeps the Stack’s state on your machine, next to the engine, and Alchemy.RandomProvider() is there for the generated password below.
A network and a volume
Section titled “A network and a volume”const network = yield* Docker.Network("app-network");const data = yield* Docker.Volume("postgres-data");Neither needs props. Alchemy names each one from the Stack, stage and resource ID, so two stages never share a volume. Changing a volume’s props later replaces it with an empty one, so settle them before it holds data.
A Redacted password
Section titled “A Redacted password”import * as Config from "effect/Config";import * as Option from "effect/Option";
const configuredPassword = yield* Config.Redacted("POSTGRES_PASSWORD").pipe( Config.option,);const password = yield* Option.match(configuredPassword, { onSome: Effect.succeed, onNone: () => Alchemy.makeRandom("PostgresPassword", { bytes: 16 }),});If POSTGRES_PASSWORD is set, it’s used as it is. Otherwise Alchemy.makeRandom generates a random password once and keeps it in the Stack’s state, so every deploy uses the same one. Either way the value is Redacted, and it never appears on the docker command line.
The container
Section titled “The container”const postgres = yield* Docker.Container("postgres", { name: "alchemy-example-postgres", image, environment: { POSTGRES_DB: "app", POSTGRES_USER: "alchemy", POSTGRES_PASSWORD: password, }, sharedMemorySize: "256m", ports: [{ external: 15432, internal: 5432 }], volumes: [ { hostPath: data.name, containerPath: "/var/lib/postgresql/data" }, ], networks: [{ name: network.name, aliases: ["postgres"] }], healthcheck: { cmd: ["CMD-SHELL", "pg_isready -U alchemy -d app"], interval: "5 seconds", timeout: "5 seconds", retries: 10, }, start: true,});image takes the RemoteImage resource itself. A hostPath set to the volume’s name mounts the volume. The postgres alias is the name other containers on app-network connect to. The health check marks the container healthy once Postgres accepts connections, and start: true runs it, rather than leaving it created and stopped.
sharedMemorySize raises /dev/shm above Docker’s 64 MB default, which Postgres runs out of on parallel queries.
Read the bound ports
Section titled “Read the bound ports”// as Stack outputs:return { container: postgres.name, hostPort: postgres.ports };
// or live, from any Effect with the Docker provider:const runtime = yield* Docker.inspectContainer("alchemy-example-postgres");ports maps each container port to its host port, here "5432/tcp" to 15432. Docker.inspectContainer reads the same attributes straight from the engine, for any container by name.
Run it
Section titled “Run it”pnpm alchemy deploybun alchemy deploynpx alchemy deployyarn alchemy deployConnect with psql postgres://alchemy:<password>@localhost:15432/app. When you’re done, alchemy destroy removes the container, network and volume, and the data with it.
- Containers, Networks and Volumes explain each piece in depth.
- Limits & security caps memory and CPU and drops privileges.
- Build images runs your own app next to the database.