我知道这个问题已经被提出并给出了答案。但这对我不起作用。我也一样我的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:
init.sql:-
create table sometable(id int);
未创建表。我只能看到数据库已创建。如何创建表,并在可能的情况下将一些数据插入表中?
答案 0 :(得分:1)
您可以按以下方式在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');