当由docker compose启动时如何创建表postgresql [英] How to create table postgresql when start by docker compose

查看:70
本文介绍了当由docker compose启动时如何创建表postgresql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被提出并给出了答案。但这对我不起作用。我也一样我的postgres容器运行正常。我检查了容器中是否存在/docker-entrypoint-initdb.d/init.sql。我使用了以下docker-compose.yml。

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:-

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?

推荐答案

您可以在init.sql中编写查询,如下所示:-

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');

这篇关于当由docker compose启动时如何创建表postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆