gpt4 book ai didi

docker - 使用 docker-compose 不存在 postgres 表

转载 作者:行者123 更新时间:2023-12-02 18:41:54 25 4
gpt4 key购买 nike

我编写了一个使用 postgres 作为数据库的简单服务器。它在不使用 docker 时工作,但是当它使用 docker compose 进行 dockerized 和编排时,我无法插入数据,因为表不存在。我确实检查了容器中的数据库,它就在那里。

以下是我的设置,最后一个是错误。希望你们能发现我错过的东西。谢谢!

docker-compose.yml

db:
build: ./database
ports:
- "5432:5432" # Bind host port 5432 to PostgreSQL port 5432
web:
build: ./app
command: npm start
ports:
- "8080:8080"
links:
- db
- redis
environment:
DB_URL: "postgres://dockeruser:@db:5432/docker?encoding=unicode&schema=public"
volumes:
- ./app:/src/*

数据库初始化
CREATE USER dockeruser WITH PASSWORD 'docker';
CREATE DATABASE docker;
CREATE TABLE people ( id SERIAL, name TEXT, email TEXT );
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO dockeruser

代码
pg.connect(conString, function(err, client, done) {
client.query('INSERT INTO public."people" (name, email) VALUES ($1, $2) RETURNING id', row, function(err, result) {
//call `done()` to release the client back to the pool
done();
if (err) return reject(err);
// console.log(result.rows[0].id);
resolve(result.rows[0].id);
});
});

错误
db_1    | ERROR:  relation "public.people" does not exist at character 13
db_1 | STATEMENT: INSERT INTO public."people" (name, email) VALUES ($1, $2) RETURNING id
web_1 | error { [error: relation "public.people" does not exist]

最佳答案

你确定你的数据库初始化脚本被调用了吗? postgresql docker 使用/docker-entrypoint-initdb.d/* 中的文件,如您在此处的 Dockerfile 中所见:https://github.com/docker-library/postgres/blob/443c7947d548b1c607e06f7a75ca475de7ff3284/9.4/docker-entrypoint.sh#L77
您可以通过仅启动数据库来测试这一点,通过运行类似

docker-compose up db


这只会启动 db 容器,然后你可以 docker exec -it in 和 psql 进入 postgres 进程,或者使用 5432 上的暴露端口
此外,您可能希望以 Dockerfile 期望的方式传入数据库、用户名和密码的初始化,作为您在同一文件中看到的环境变量

Environment VariablesThe PostgreSQL image uses several environment variables which are easy to miss. While none of the variables are required, they may significantly aid you in using the image.

POSTGRES_PASSWORDThis environment variable is recommended for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable. In the above example, it is being set to "mysecretpassword".

POSTGRES_USERThis optional environment variable is used in conjunction with POSTGRES_PASSWORD to set a user and its password. This variable will create the specified user with superuser power and a database with the same name. If it is not specified, then the default user of postgres will be used.

POSTGRES_DBThis optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.


https://hub.docker.com/_/postgres/

关于docker - 使用 docker-compose 不存在 postgres 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35707379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com