Initialise Postgresql Container with init SQL script


Initialise Postgresql Container with init SQL script

Initialise Postgresql Container with init SQL script

Prerequisite

  1. Install Docker
  2. install docker-compose

Init SQL script with postgresql container

  • Create a docker-compose.yml with the following
---
version: '3.6'
services:
  postgresql:
    image: postgres:11.3
    container_name: postgres
    volumes:
      # Uncomment below to maintain the peristant data
      # - platops-data:/var/lib/postgresql/data/
      # Uncomment bellow to intialize the container with data by creating the respective file
      # - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    environment:
      - POSTGRES_USER=postgress
      - POSTGRES_PASSWORD=postgress
    ports: ['5432:5432']
    networks: ['stack']
    healthcheck:
      test: curl -s https://localhost:5432 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 30s
      timeout: 10s
      retries: 5
  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

volumes:
  platops-data:
networks:
  stack:

Try in PWD

  • Created a init.sql with the following
CREATE USER platops WITH PASSWORD 'platops';
CREATE DATABASE platopsdb;
GRANT ALL PRIVILEGES ON DATABASE platopsdb TO platops;
  • RUN container with docker-compose up -d, this will start your container with initializing the database with the above scripts.

See that it’s working

docker logs -f postgres

# OR

docker logs postgres

comments powered by Disqus