darfdx
darfdx
TTCTheo's Typesafe Cult
Created by darfdx on 1/21/2025 in #questions
[SOLVED] New to Docker, can't connect from localhost to DB on container
I am running Docker on Windows via Docker Desktop. I am trying to follow a tutorial and one of the first steps is connecting to a Postgres DB on Docker with Drizzle, but my I can't connect to it and I get an error saying the DB doesn't exist. I have this compose.yml file:
services:
db:
image: postgres:17
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"

volumes:
pgdata:
services:
db:
image: postgres:17
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"

volumes:
pgdata:
This script for testing the connection:
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import env from '@/env';

const pool = new Pool({
connectionString: env.DATABASE_URL,
});

export const db = drizzle(pool);

// Test the connection
const result = await db.execute('SELECT 1');
console.log(result);
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import env from '@/env';

const pool = new Pool({
connectionString: env.DATABASE_URL,
});

export const db = drizzle(pool);

// Test the connection
const result = await db.execute('SELECT 1');
console.log(result);
And this is my DB URL on env:
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=betternewsdb

DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@localhost:5432/${DB_NAME}
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=betternewsdb

DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@localhost:5432/${DB_NAME}
Error stack:
bun run .\server\db\index.ts
40 | res = resolve
41 | rej = reject
42 | }).catch((err) => {
43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
44 | // application that created the query
45 | Error.captureStackTrace(err)
^
error: database "betternewsdb" does not exist
length: 98,
severity: "FATAL",
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
dataType: undefined,
constraint: undefined,
file: "postinit.c",
routine: "InitPostgres",
code: "3D000"
bun run .\server\db\index.ts
40 | res = resolve
41 | rej = reject
42 | }).catch((err) => {
43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
44 | // application that created the query
45 | Error.captureStackTrace(err)
^
error: database "betternewsdb" does not exist
length: 98,
severity: "FATAL",
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
dataType: undefined,
constraint: undefined,
file: "postinit.c",
routine: "InitPostgres",
code: "3D000"
If I connect on my local psql terminal, I can't connect to the DB and get a similar error. If I open docker's bash, I can connect to it. Am I doing something wrong?
21 replies