![](/img/trans.png)
[英]password authentication failed for user "root", postgresSQL and docker
[英]Problems with running PostgresSQL together with node/express in Docker. FATAL: no PostgreSQL user name specified in startup packet
我正在嘗試在 Docker 中運行開發環境。
我有一個docker-compose
文件,其中包含 4 個服務:前端、postgres、pgadmin4 和 node/express。 如果我在 Docker 和 Express 本地運行 postgres,我可以連接到數據庫。 如果我嘗試在 Docker 中運行 Express 和 Postgres,Express 正在工作,我也可以通過pgadmin4
連接到 postgres,但我得到:
FATAL: no PostgreSQL user name specified in startup packet
當我嘗試連接到數據庫的端點時。
在 Google 中搜索后,我認為這與由於某種原因未正確設置環境變量有關。 另外,我對主機名如何工作以及在哪里使用什么主機名有點困惑? 我的機器的主機名? 本地主機? docker 服務的名稱? 無論如何,這是我的配置:
docker-compose.yml
version: '3'
services:
frontend:
build: ./frontend/
command: npm run start
volumes:
- ./frontend/:/usr/app/
- ./frontend/node_modules
ports:
- '3000:3000'
db:
image: postgres
ports:
- '5432:5432'
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=exampledb
db_admin:
image: dpage/pgadmin4
ports:
- '5555:80'
environment:
- PGADMIN_DEFAULT_EMAIL=asdf
- PGADMIN_DEFAULT_PASSWORD=asdf
depends_on:
- db
api:
build: ./backend/api/
command: npm run dev
volumes:
- ./backend/api/:/usr/app/
- /backend/api/node_modules
ports:
- '3333:3333'
depends_on:
- db
連接postgres的JS代碼
const { Pool } = require('pg')
const client = new Pool({
host: 'db',
port: '5432',
username: 'user',
password: 'password',
database: 'exampledb'
})
module.exports = {
query: (text, params) => client.query(text, params),
}
謝謝!
在您的池連接上,它應該如下所示:
const client = new Pool({
host: 'db',
port: '5432',
user: 'user',
password: 'password',
database: 'exampledb'
})
module.exports = {
query: (text, params) => client.query(text, params),
}
將用戶username
更改為user
作為您的密鑰,您應該是金色的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.