I am facing an issue with connecting to my database running on a Docker container using a postgres image:
docker run --name postgres-container -p 2345:2345 -e POSTGRES_PASSWORD=password123 -e POSTGRES_USER=admin -d postgres
The TypeScript code I have is in a .ts file.
app.get("/hello", async (request: Request, response: Response) => {
const pool = new Pool({
user: 'admin',
host: 'localhost',
database: 'betsdb',
password: 'password123',
port: 2345,
});
console.log("trying connection");
const client = await pool.connect();
console.log("succeeded");
});
When I run "await pool.connect", I encounter the error "Error: Connection terminated unexpectedly".
In Docker, I can interact with the DB without any issues, the password works fine. My container is running at http://localhost:2345 as verified in Docker Desktop.
I would really appreciate any help or suggestions. I have been searching for a solution for hours on Stack Overflow and tried different solutions suggested by others but nothing has worked so far.