【问题标题】:Issue with Docker Desktop & Node Postgres connectionDocker 桌面和节点 Postgres 连接问题
【发布时间】:2021-06-08 15:44:12
【问题描述】:

嘿,我一直在努力解决这个问题,希望社区中的人能提供帮助。

我目前使用的是 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"

我尝试手动向数据库添加第二个超级用户,以防创建的超级用户出现问题并交换环境变量,但最终结果相同。

我一定是遗漏了一些奇怪的东西......任何建议表示赞赏。

供参考:
码头工人撰写文件:https://github.com/dan-mcm/cnc-db/blob/master/db-setup/docker-compose.yaml
pg 池文件: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

【问题讨论】:

  • 从您的.env 文件中删除所有",然后重试
  • 试一试——结果相同
  • 你也可以发布容器日志吗?
  • 将日志添加到初始帖子 Tarun
  • 你能不能用postgres的用户名和密码试试

标签: node.js postgresql docker docker-compose docker-desktop


【解决方案1】:

所以经过一番挖掘后,我检查了端口,发现本地安装了两个旧版 postgres,默认情况下正在使用 v10 和 v13 覆盖暴露在端口 5432 上的 docker 容器。为了解决这个问题,我更改了Docker 容器上的转发端口改为通过端口 5444 访问数据库。

感谢大家的建议——调试起来很烦人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 2019-07-06
    • 2020-08-26
    • 2018-03-21
    • 2011-04-11
    • 1970-01-01
    • 2022-01-01
    • 2020-03-08
    相关资源
    最近更新 更多