How to create table postgresql when start by docke

2020-05-01 09:49发布

I know this question is already asked and also answer given. But it is not working for me. I follow the same. My postgres container is running fine. I checked inside the container that /docker-entrypoint-initdb.d/init.sql exist.I used the following 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);   

No table created. I can see only the database is created. How do I create a table and also if possible insert some data into the table?

1条回答
甜甜的少女心
2楼-- · 2020-05-01 10:29

You can write your queries inside init.sql, in this squence:-

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');
查看更多
登录 后发表回答