[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.
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 connectionconst 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 connectionconst result = await db.execute('SELECT 1');console.log(result);
bun run .\server\db\index.ts40 | res = resolve41 | rej = reject42 | }).catch((err) => {43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the44 | // application that created the query45 | 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.ts40 | res = resolve41 | rej = reject42 | }).catch((err) => {43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the44 | // application that created the query45 | 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?
Solution
you can try running this on windows to see if anything is also taking up that port.
netstat -an | find "5432"
netstat -an | find "5432"
, cuz its possible that an existing postgres instance is already running but your docker compose just couldn't exposed it.