Skip to content

logambigaik/Dockerpostgres

Repository files navigation

Prerequisite:

  >> docker
  >> docker-compose
    
        sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
        sudo chmod +x /usr/local/bin/docker-compose
        sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
        docker-compose --version
        
  >> start docker 

Docker file for Postgres image:

==============================

Dockerfile:

FROM postgres:11.5
ENV POSTGRES_HOST_AUTH_METHOD=trust
ENV POSTGRES_USERNAME='flaskdb'
ENV POSTGRES_PASSWORD='Password1'
COPY ./docker-entrypoint-initdb.d/sql1.sql docker-entrypoint-initdb.d/sql1.sql

docker-entrypoint-initb.d:

CREATE USER flaskdb WITH ENCRYPTED PASSWORD 'Password1';
CREATE DATABASE flaskdb;
GRANT ALL PRIVILEGES ON DATABASE flaskdb TO flaskdb;

Reference:

        #Establishing the connection
          conn = psycopg2.connect(
                database='flaskdb', user='flaskdb', password='Password1', host='mydb', port= '5432'
          )

        # Note : host is db-container name not localhost or 127.0.0.1 in docker

#requirements.txt

  flask
  psycopg2

Python dockerfile:

      @@ With Virtual
      
      FROM python:latest
      WORKDIR /code
      RUN python3 -m pip install --upgrade pip
      RUN python3 -m pip install virtualenv
      RUN python3 -m venv virtual
      RUN . virtual/bin/activate
      ADD requirements.txt requirements.txt
      RUN pip install -r requirements.txt
      COPY app.py app.py
      COPY templates/index.html  templates/index.html
      CMD ["python", "app.py"]
      
      @@ Another one without virtual:
      
      FROM python:latest
      WORKDIR /code
      ADD requirements.txt requirements.txt
      RUN pip install -r requirements.txt
      COPY app.py app.py
      COPY templates/    templates/
      CMD ["python", "app.py"]

Docker compose:

      version: "3.8"
      services:
      app:
        image: web:v1
        container_name: app
        ports:
        - 5000:5000
        links:
        - db
        networks:
        - python-postgres

      db:
        image: mydb:v1
        container_name: mydb
        restart: always
        ports:
        - 5432:5432
        environment:
        - POSTGRES_HOST_AUTH_METHOD=trust
        - PGDATA='/var/lib/postgresql/data'
        volumes:
        - /var/postgres/data:/var/lib/postgres/data
        networks:
        - python-postgres

      networks:
      python-postgres:
         driver : bridge
          name: python-postgres


   
@@@Docker-compose file:

image

@@@mydb log after docker-compose up:

image

@@@mydb-docker-compose up:

image

@@@Browser

image

 ######################################################################################################
 ##  NOTES
 ######################################################################################################
 
 # Postgres SQL:   

docker run --name db -e POSTGRES_PASSWORD=Password1 -d -p 5432:5432 db:v1
docker exec -it db bash
psql -U flaskdb
postgres=# \l
postgres=# \c flaskdb   ---to connect
postgres=# select * from userdetail;

\q to quit

  #In case connection refuse in local:
      1. Modify two configure files
          # vi /var/lib/pgsql/data/postgresql.conf
            Replace the line:
            listen_addresses = 'localhost'  -> listen_addresses = '*'


      2. Modify two configure files
          # vi /var/lib/pgsql/data/postgresql.conf
          Replaced the line:
          listen_addresses = 'localhost'  -> listen_addresses = '*'
          
          # vi /var/lib/pgsql/data/pg_hba.conf
           Add the line at the very end:
          host all all 0.0.0.0/0 trust
          (If IPv6: host all all ::/0 trust) 
  
      3. Restart the database service
          # service postgresql restart

pg_dump:

      docker exec container-name pg_dump -U db_user dbname > dbdump_file
      docker exec mydb pg_dump -U flaskdb flaskdb > 2.sql

image

  docker exec mydb pg_dump -U flaskdb flaskdb | gzip > 2.gz

image

  unzip with : gzip -d 2.gz

image image

    docker exec mydb pg_dumpall -U postgres --schema-only > definitiononly.sql
    
    In execution of docker :

image

image image image

pg_dump with --roles-only:

  docker exec mydb pg_dumpall -U postgres --roles-only > allroles.sql

image

pg_dump with --tablespaces-only :

        docker exec mydb pg_dumpall -U flaskdb --tablespaces-only > tb.sql

image

pg_dump syntax for reference:

    pg_dump -U postgres -W -F t dvdrental > c:\pgbackup\dvdrental.tar


  -U postgres:  specifies the user to connect to the PostgreSQL database server. We used the postgres in this example.

  -W:  forces pg_dump to prompt for the password before connecting to the PostgreSQL database server. After you hit enter, pg_dump will prompt for the password of postgres user.

  -F : specifies the output file format that can be one of the following:

      c: custom-format archive file format
      d: directory-format archive
      t: tar
      p: plain-text SQL script file).
  In this example, we use  -F t to specify the output file as a tar file.

dvdrental: is the name of the database that you want to back up.

  > c:\pgbackup\dvdrental.tar is the output backup file path.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages