我無法驗證我的 postgres 資料庫。我正在使用 docker-compose。請在下面找到相關檔案和日志。我的連接字串有問題嗎?
碼頭工人-compose.yml
version: "3.8"
services:
app:
build:
context: ./
target: dev
volumes:
- .:/src
command: npm run start:dev
ports:
- "3000:3000"
depends_on:
- postgres
environment:
DATABASE_URL: postgres://votetracker@postgres/codes
POSTGRES_PASSWORD: password
NODE_ENV: development
DEBUG: nodejs-docker-express:*
postgres:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: codes
資料庫.js
const { Sequelize } = require("sequelize");
const sequelize = new Sequelize("postgres://user:pass@database:5432/codes");
const authenticate = async () => {
try {
await sequelize.authenticate();
console.log("Connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}
};
module.exports = { authenticate };
運行時docker-compose up,這是輸出:
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
...
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
...
postgres_1 | server started
postgres_1 | CREATE DATABASE
...
postgres_1 | PostgreSQL init process complete; ready for start up.
...
postgres_1 | 2022-01-23 17:32:03.824 UTC [1] LOG: database system is ready to accept connections
...
app_1 | Your app is running on port 3000
app_1 | Unable to connect to the database: ConnectionError [SequelizeConnectionError]: getaddrinfo EAI_AGAIN database
uj5u.com熱心網友回復:
你快到了。當您需要連接到 docker-compose 橋接網路上的另一個容器時,您可以使用服務名稱作為主機名。
所以而不是
const sequelize = new Sequelize("postgres://user:pass@database:5432/codes");
你需要
const sequelize = new Sequelize("postgres://user:pass@postgres:5432/codes");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420388.html
標籤:
下一篇:使用容器鏡像進行lamda函式
