我知道这个问题已经被问过并且也给出了答案。但这对我不起作用。我也遵循同样的做法。我的 postgres 容器运行良好。我在容器内部检查了 /docker-entrypoint-initdb.d/init.sql 是否存在。我使用了以下 docker-compose.yml。
version: "3"
services:
postgres:
image: postgres:latest
network_mode: bridge
container_name: postgres
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- POSTGRES_PASSWORD=admin
- POSTGRES_USER=postgres
- POSTGRES_DB=dev
restart: unless-stopped
# APP
profile_editor2:
build:
context: .
dockerfile: ./Dockerfile
network_mode: bridge
container_name: profile_editor2
volumes:
- ./image:/app/image
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
初始化.sql:-
create table sometable(id int);
没有创建表。我只能看到数据库已创建。如何创建表并在可能的情况下向表中插入一些数据?
您可以按以下顺序在 init.sql 中编写查询:-
DROP DATABASE IF EXISTS test_db;
CREATE DATABASE test_db;
\c test_db;
CREATE TABLE Role(
RoleID SERIAL PRIMARY KEY,
RoleName varchar(50),
);
insert into Role(RoleName)
values ('Admin'),('User');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)