Docker Desktop & Node Postgres 连接问题



大家好,我一直在为这个问题绞尽脑汁,希望社区里有人能帮忙。

我目前使用的是windows 10,以前使用Docker工具箱进行开发设置,升级到Docker桌面后,我遇到了问题。

我通常使用以下方式启动带有docker-compose文件的postgres db:

postgres:
container_name: cnc-matches
image: postgres:12.1-alpine
ports:
- '5432:5432'
environment:
POSTGRES_DB: cnc-matches
POSTGRES_USER: danku
POSTGRES_PASSWORD: cnc123

使用命令docker-compose up -d.

这似乎可以工作,但是当我试图从我的应用程序使用像这样的节点pg库连接到它时

const { Pool } = require('pg');
const dotenv = require('dotenv').config();
pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT
});

我得到错误日志:

error: password authentication failed for user "danku"

应用程序的。env文件正在使用这些值

DB_USER="danku"
DB_PASSWORD="cnc123"
DB_HOST="127.0.0.1"
DB_NAME="cnc-matches"
DB_PORT="5432"

我尝试手动添加第二个超级用户到DB,以防创建的超级用户有问题,并在环境变量周围交换,但最终结果相同。

我一定错过了什么奇怪的东西…如有任何建议,不胜感激。


docker-compose file: https://github.com/dan-mcm/cnc-db/blob/master/db-setup/docker-compose.yaml
pg pool file: https://github.com/dan-mcm/cnc-db/blob/master/utils/dbQueries.js#L18

我也试着用url的格式来使用它,比如

STAGING_DB_URL="postgres://danku:cnc123@127.0.0.1:5432/cnc-matches"

但是同样的错误。

根据请求,这里是来自docker容器的日志

Attaching to cnc-matches
cnc-matches | ****************************************************
cnc-matches | WARNING: No password has been set for the database.
cnc-matches | This will allow anyone with access to the
cnc-matches | Postgres port to access your database. In
cnc-matches | Docker's default configuration, this is
cnc-matches | effectively any other container on the same
cnc-matches | system.
cnc-matches |
cnc-matches | Use "-e POSTGRES_PASSWORD=password" to set
cnc-matches | it in "docker run".
cnc-matches | ****************************************************
cnc-matches | The files belonging to this database system will be owned by user "postgres".
cnc-matches | This user must also own the server process.
cnc-matches |
cnc-matches | The database cluster will be initialized with locale "en_US.utf8".
cnc-matches | The default database encoding has accordingly been set to "UTF8".
cnc-matches | The default text search configuration will be set to "english".
cnc-matches |
cnc-matches | Data page checksums are disabled.
cnc-matches |
cnc-matches | fixing permissions on existing directory /var/lib/postgresql/data ... ok
cnc-matches | creating subdirectories ... ok
cnc-matches | selecting dynamic shared memory implementation ... posix
cnc-matches | selecting default max_connections ... 100
cnc-matches | selecting default shared_buffers ... 128MB
cnc-matches | selecting default time zone ... UTC
cnc-matches | creating configuration files ... ok
cnc-matches | running bootstrap script ... ok
cnc-matches | sh: locale: not found
cnc-matches | 2021-03-10 16:43:29.110 UTC [31] WARNING: no usable system locales were found
cnc-matches | performing post-bootstrap initialization ... ok
cnc-matches | syncing data to disk ... ok
cnc-matches |
cnc-matches |
cnc-matches | Success. You can now start the database server using:
cnc-matches |
cnc-matches | pg_ctl -D /var/lib/postgresql/data -l logfile start
cnc-matches |
cnc-matches | initdb: warning: enabling "trust" authentication for local connections
cnc-matches | You can change this by editing pg_hba.conf or using the option -A, or
cnc-matches | --auth-local and --auth-host, the next time you run initdb.
cnc-matches | waiting for server to start....2021-03-10 16:43:29.856 UTC [36] LOG: starting PostgreSQL 12.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
cnc-matches | 2021-03-10 16:43:29.871 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
cnc-matches | 2021-03-10 16:43:29.900 UTC [37] LOG: database system was shut down at 2021-03-10 16:43:29 UTC
cnc-matches | 2021-03-10 16:43:29.906 UTC [36] LOG: database system is ready to accept connections
cnc-matches | done
cnc-matches | server started
cnc-matches | CREATE DATABASE
cnc-matches |
cnc-matches |
cnc-matches | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
cnc-matches |
cnc-matches | waiting for server to shut down...2021-03-10 16:43:30.175 UTC [36] LOG: received fast shutdown request
cnc-matches | .2021-03-10 16:43:30.193 UTC [36] LOG: aborting any active transactions
cnc-matches | 2021-03-10 16:43:30.193 UTC [36] LOG: background worker "logical replication launcher" (PID 43) exited with exit code 1
cnc-matches | 2021-03-10 16:43:30.193 UTC [38] LOG: shutting down
cnc-matches | 2021-03-10 16:43:30.231 UTC [36] LOG: database system is shut down
cnc-matches | done
cnc-matches | server stopped
cnc-matches |
cnc-matches | PostgreSQL init process complete; ready for start up.
cnc-matches |
cnc-matches | 2021-03-10 16:43:30.294 UTC [1] LOG: starting PostgreSQL 12.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
cnc-matches | 2021-03-10 16:43:30.294 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
cnc-matches | 2021-03-10 16:43:30.294 UTC [1] LOG: listening on IPv6 address "::", port 5432
cnc-matches | 2021-03-10 16:43:30.313 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
cnc-matches | 2021-03-10 16:43:30.340 UTC [47] LOG: database system was shut down at 2021-03-10 16:43:30 UTC
cnc-matches | 2021-03-10 16:43:30.347 UTC [1] LOG: database system is ready to accept connections

因此,在进行了一堆挖掘之后,我检查了端口,看到两个遗留的postgres本地安装v10和v13被默认使用覆盖端口5432上暴露的docker容器。为了解决这个问题,我将docker容器上的转发端口更改为通过端口5444访问DB。

感谢所有的建议-非常讨厌调试!

最新更新