[英]Cannot connect to postgresql container using npm pg in a docker-compose scenario
I am using npm pg
( "pg": "^7.18.2", "pg-native": "^3.0.0"
in my package.json), at which I am new, to connect my newbie node
app to a postgresql
database.我正在使用
npm pg
( "pg": "^7.18.2", "pg-native": "^3.0.0"
我的 package.json 中的"pg": "^7.18.2", "pg-native": "^3.0.0"
),我是新手,将我的新手node
应用程序连接到postgresql
数据库。 I use docker-compose
to start up everything using this configuration:我使用
docker-compose
使用此配置启动所有内容:
version: '3'
services:
postgres:
image: 'postgres:latest'
environment:
- POSTGRES_USER=postgres_user
- POSTGRES_PASSWORD=postgres_password
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres_user"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: 'redis:latest'
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '8000:80'
depends_on:
- fibclient
- fibserver
fibserver:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- './server:/app'
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- PGUSER=postgres_user
- PGPASSWORD=postgres_password
- PGHOST=postgres
- PGPORT=5432
- PGDATABASE=postgres
depends_on:
- redis
- postgres
- fibworker
ports:
- '9229:9229'
fibclient:
build:
dockerfile: Dockerfile.dev
context: ./client
volumes:
- './client:/app'
fibworker:
build:
dockerfile: Dockerfile.dev
context: ./fibworker
volumes:
- './fibworker:/app'
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
ports:
- '9230:9229'
depends_on:
- redis
As you can imagine fibserver
is the backend app trying to connect to postgres
service which is hosting the postgresql
database.您可以想象
fibserver
是后端应用程序,它试图连接到托管postgresql
数据库的postgres
服务。 Sadly, fibserver
app is always getting this error while connecting to the database:可悲的是,
fibserver
应用程序在连接到数据库时总是收到此错误:
fibserver_1 | Error: Connection terminated due to connection timeout
fibserver_1 | at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:255:9)
fibserver_1 | at Object.onceWrapper (events.js:420:28)
fibserver_1 | at Connection.emit (events.js:314:20)
fibserver_1 | at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:78:10)
fibserver_1 | at Socket.emit (events.js:314:20)
fibserver_1 | at emitCloseNT (net.js:1657:8)
fibserver_1 | at processTicksAndRejections (internal/process/task_queues.js:79:21)
fibserver_1 | at runNextTicks (internal/process/task_queues.js:62:3)
fibserver_1 | at listOnTimeout (internal/timers.js:520:9)
fibserver_1 | at processTimers (internal/timers.js:494:7)
I double-checked my docker-compose
file, if services are aware of each other ( fibserver
can ping postgres
), npm pg
configuration, docs and examples, and everything looks ok to me.我仔细检查了我的
docker-compose
文件,如果服务知道彼此( fibserver
可以 ping postgres
)、 npm pg
配置、文档和示例,我觉得一切正常。 Still missing something.还是少了点什么。 This is where you can find the whole stuff.
这是在那里你可以找到整个东西。
UPDATE: I put a simple circuit-breaker in my fibserver
: when he fails to connect to database it wait for a while and tries again.更新:我在
fibserver
放置了一个简单的断路器:当他无法连接到数据库时,它会等待一段时间并再次尝试。 Well, fibserver
did a lot of attempts to connect do postgres
service, even after 10 minutes.好吧,
fibserver
做了很多尝试连接postgres
服务,即使在 10 分钟之后。 Could be my usage of npm pg
library to cause this issue?可能是我对
npm pg
库的使用导致了这个问题? I also update the master if anyone wants to have a look.如果有人想看,我也会更新主人。 Thankyou.
谢谢你。
UPDATE: I installed psql into fibserver container and I managed to connect to postgres service succesfully, I think it's a matter of how I am using the pg client.更新:我将 psql 安装到 fibserver 容器中,并且成功连接到 postgres 服务,我认为这与我如何使用 pg 客户端有关。 This was something I should have tried before.
这是我之前应该尝试过的。
Finally having some time to go further this issue.终于有时间进一步讨论这个问题了。 I uninstall the two
pg
modules in my fibserver application and install pg again provides me a new version of npm
pg
module (8.3.3) without having the native part which I probably installed by mistake.我在 fibserver 应用程序中卸载了两个
pg
模块并再次安装 pg 为我提供了一个新版本的npm
pg
模块(8.3.3),而没有我可能错误安装的本机部分。 Single javascript pg
library does the job.单个 javascript
pg
库可以完成这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.