3

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?

0

1 Answer 1

9

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');
Sign up to request clarification or add additional context in comments.

5 Comments

I will create a database and role. Not the table. I can create a database and default user postgres is fine for me. I couldn't create table.
it will create a table Role inside database test_db.
Those r created by environment: - POSTGRES_PASSWORD=admin - POSTGRES_USER=postgres - POSTGRES_DB=dev I need to create a table .
are you checking "sometable" inside "\c dev" database ? Your init.sql is fine.
yes. "\c dev" and there is no table created. Looks like postgres didn't pick up that init.sql.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.