I have an app with postgres as db, sequelize, and express, and whenever it receives a db query, it just stays there forever, no logging or anything
I run postgres in a container which I can connect to through GUI normally
When I swapped it for sqlite, it worked perfectly the application
here is the relevant piece of code
const databaseURL =
process.env.DATABASE_URL ||
"postgres://postgres:[email protected]:5432/postgres";
console.log(databaseURL)
const db = new Sequelize(databaseURL, { logging: console.log });
on docker compose
dc ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------
baity-backend_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp,:::5432->5432/tcp
logs
> DEBUG=express:* node index.js
express:application set "x-powered-by" to true +0ms
express:application set "etag" to 'weak' +1ms
express:application set "etag fn" to [Function: generateETag] +1ms
express:application set "env" to 'development' +0ms
express:application set "query parser" to 'extended' +1ms
express:application set "query parser fn" to [Function: parseExtendedQueryString] +0ms
express:application set "subdomain offset" to 2 +0ms
express:application set "trust proxy" to false +0ms
express:application set "trust proxy fn" to [Function: trustNone] +0ms
express:application booting in development mode +0ms
express:application set "view" to [Function: View] +0ms
express:application set "views" to '/home/omar/workspace/js/baity-backend/views' +0ms
express:application set "jsonp callback name" to 'callback' +0ms
postgres://postgres:[email protected]:5432/postgres
Update 1:
version: "3.9"
services:
web:
build:
dockerfile: Dockerfile
context: ./frontend
env_file:
- ./frontend/.env
ports:
- "8080:8080"
- "3000:3000"
stdin_open: true
volumes:
- ./frontend:/opt/web
app:
build: .
ports:
- "4000:4000"
volumes:
- .:/code
- /code/node_modules
restart: always
command: npm start
env_file:
- .env
links:
- db
db:
image: postgres:11.14-bullseye
ports:
- "5432:5432"
env_file:
- .env
volumes:
- ./.data/db:/var/lib/postgresql/data
FROM node:14.18.2-bullseye
WORKDIR /code
COPY package*.json ./
RUN npm install -g nodemon
RUN npm install
COPY . .
Package.json
{
"name": "server",
"version": "1.0.0",
"description": "Node based server for real eastate website",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo "Error: no test specified" && exit 1"
},
"author": "xxx <[email protected]>",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"bcrypt": "^4.0.1",
"cloudinary": "^1.20.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-form-data": "^2.0.12",
"express-formidable": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.4",
"node-cron": "^2.0.3",
"pg": "^7.18.2",
"sequelize": "^5.21.5",
"sequelize-auto-migrations": "^1.0.3",
"sqlite3": "^5.0.2",
"stripe": "^8.35.0"
}
}
node: 14.18.2
DATABASE_URL=postgres://postgres:postgres@db:5432/postgres
3
Answers
I think it is your "0.0.0.0:5432".
If local, it should be just "localhost:5432".
If deployed server is remote, it should be a certain IP address XXX.XXX.XXX.XXX:5432.
If deployed server is home network, it should be "192.168.0.XXX:5432".
Check your postgres network configuration
https://youtu.be/Erqp4C3Y3Ds
In the node.js container when trying to connect to the database it will try to connect to itself "0.0.0.0" on port 5432 where there is no Postgres running. Assuming you have a container for nodejs backend app, and postgres instance for database.
You need to guide the nodejs container where’s the Postgres instance.
You need to change the host
"0.0.0.0:5432"
to yourdocker-name
.In your case, you can change that variable databaseURL or set variable DATABASE_URL
to "
postgresql://postgres:postgres@your_machine_ip:5432/postgres
"As you’re using docker-compose:
as you can see the DATABASE_URL will use the "app-database" which is the name of postgres container.
After a quick test: according to your posted
package.json
– you are using an incompatible version of thepg
module and Node.Js 14.See this issue for more context.
The solution is: make sure you are running the latest
pg
version.For example with yarn:
If this is not possible for any reason – downgrading to Node 12 should also work. This can be easily tested in your
Dockerfile
.