首页 > 解决方案 > Docker 桌面和节点 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"

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

我必须在这里遗漏一些奇怪的东西......任何建议表示赞赏。

供参考:
docker-compose 文件: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

标签: node.jspostgresqldockerdocker-composedocker-desktop

解决方案


因此,经过一番挖掘后,我检查了端口,并看到两个传统的 postgres 安装本地 v10 和 v13 默认使用覆盖在端口 5432 上公开的 docker 容器。为了解决这个问题,我更改了转发端口docker 容器改为通过端口 5444 访问数据库。

感谢所有的建议 - 调试非常烦人!


推荐阅读