Skip to content

Instantly share code, notes, and snippets.

@devinschumacher
Last active September 16, 2024 23:26
Show Gist options
  • Save devinschumacher/e93f5ea560bcfa0c8547329aabc1d2b7 to your computer and use it in GitHub Desktop.
Save devinschumacher/e93f5ea560bcfa0c8547329aabc1d2b7 to your computer and use it in GitHub Desktop.
Docker compose file to run postgresql database locally
NODE_ENV=development
DB_HOST=localhost
DB_USER=<username>
DB_PASSWORD=<password>
DB_NAME=<db_name>
DB_PORT=5432
DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
services:
db:
image: postgres:16.2
environment:
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_USER=${DB_USER}
- POSTGRES_DB=${DB_NAME}
ports:
- ${DB_PORT}:5432
volumes:
- ./db-data:/var/lib/postgresql/data
// src/env.ts
// * use zod to make working with our .env typesafe
import { config } from "dotenv";
import { expand } from "dotenv-expand";
import { ZodError, z } from "zod";
const stringBoolean = z.coerce.string().transform((val) => {
return val === "true";
}).default("false");
const EnvSchema = z.object({
NODE_ENV: z.string().default("development"),
DB_HOST: z.string(),
DB_USER: z.string(),
DB_PASSWORD: z.string(),
DB_NAME: z.string(),
DB_PORT: z.coerce.number(),
DATABASE_URL: z.string(),
DB_MIGRATING: stringBoolean,
DB_SEEDING: stringBoolean,
});
export type EnvSchema = z.infer<typeof EnvSchema>;
expand(config());
try {
EnvSchema.parse(process.env);
} catch (error) {
if (error instanceof ZodError) {
let message = "Missing required values in .env:\n";
error.issues.forEach((issue) => {
message += issue.path[0] + "\n";
});
const e = new Error(message);
e.stack = "";
throw e;
} else {
console.error(error);
}
}
export default EnvSchema.parse(process.env);
@devinschumacher
Copy link
Author

devinschumacher commented Sep 16, 2024

$ docker compose up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment