Thursday 18 August 2022

How to Dockerize PostgreSQL ? How to use docker postgresql ?

 


Most Used Docker Commands:Interview Question

Pre-Requisite: 


What is PostgreSQL?

PostgreSQL has object-oriented features for handling unstructured data, it is widely used as a Relational Database. 

PostgreSQL supports both SQL and JSON to implement relational and non-relational queries on data present inside databases. In other words, with PostgreSQL, you can write SQL commands to process data present in tables that belong to the respective database servers. Because of its vast features and functionalities, PostgreSQL is reportedly ranked 4th among the most popular databases worldwide.

Install PostgreSQL on Docker

Assuming there is no Docker image that suits your needs in the Docker Hub, you can create one yourself.

Start by creating a new Dockerfile:

Note: This PostgreSQL setup is for development-only purposes. Refer to the PostgreSQL documentation to fine-tune these settings so that they are suitably secure.


 

If you want to use the above code kindly download it from below github repository:
https://github.com/sidharth8891/dockerhelp/blob/main/dockerFilePostgreSql

Build an image from the Dockerfile and assign it a name.

$ docker build -t eg_postgresql .

Run the PostgreSQL server container (in the foreground):

$ docker run --rm -P --name pg_test eg_postgresql

There are two ways to connect to the PostgreSQL server. You can use Link Containers, or we can access it from our host (or the network).

Note: The --rm removes the container and its image when the container exits successfully.

Use container linking

Containers can be linked to another container’s ports directly used --link remote_name:local_alias in the client’s docker run. This sets up a number of environmental variables that can then be used to connect:

$ docker run --rm -t -i --link pg_test:pg eg_postgresql bash

postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password

**********

***********

Connect from your host system

Assuming you have the postgresql-client installed, you can use the host-mapped port to test it as well. You need to use docker ps this to find out what local host port the container is mapped to first:

$ docker ps

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
5e24362f27f6        eg_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test

$ psql -h localhost -p 49153 -d docker -U docker --password

Test the database

Once you have authenticated and have a docker =# prompt, you can create a table and populate it.



If you want to use the above code, kindly download it from the below github link:
https://github.com/sidharth8891/dockerhelp/blob/main/TestTheDatabase

Use container volumes

You can use the defined volumes to inspect the PostgreSQL log files and to backup your configuration and data:

$ docker run --rm --volumes-from pg_test -t -i busybox sh

/ # ls
bin      etc      lib      linuxrc  mnt      proc     run      sys      usr
dev      home     lib64    media    opt      root     sbin     tmp      var
/ # ls /etc/postgresql/9.3/main/
environment      pg_hba.conf      postgresql.conf
pg_ctl.conf      pg_ident.conf    start.conf
/tmp # ls /var/log
ldconfig    postgresql
  • Interview questions for SDET/Automation Testing:
  • Top 21 GIT interview questions
  • Top 30 Selenium Webdriver Interview Q&A 
  • Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

    TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122
    Entire course content can be found below:  COURSE CONTENT


  • Interview questions for SDET/Automation Testing:
  • Top 21 GIT interview questions
  • Top 30 Selenium Webdriver Interview Q&A 
  • Top 20 API Testing Interview Q&ASelenium Interview Question SET-4 
  • Selenium Interview Question SET-1
  • Selenium Interview Question SET-2
  • Selenium Interview Questions SET-3
  • Selenium Interview Question SET-4

      
  •        Check GIT commands for Interview preparation SET-1:

    GIT Interview Question 1 to 11

    Check GIT commands for Interview preparation SET-2:

    GIT Interview Question 12 to 21

    Check GIT commands for Interview preparation SET-3:

    GIT Interview Question 21 to 30

    Check below link for question and answers with code:

    Top API Interview Question 1-10

    *************************************************

    SeleniumWebdriver Automation Testing Interview Questions:

    API Testing Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Rest-API

     
    Kubernetes Interview Question Set
    https://automationreinvented.blogspot.com/search/label/Kubernetes

     
    Docker Interview Question Set
    https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

     
    Linux Interview question Set
    https://automationreinvented.blogspot.com/search/label/Linux

     
    Automation Testing/SDET Framework Design
    https://automationreinvented.blogspot.com/search/label/FrameworkDesign


    Java Related Interview Question Set

    https://automationreinvented.blogspot.com/search/label/Java


    GIT Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/GIT


    Coding Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Coding%20Questions
    Mobile Testing Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

    Python Interview Question Set for QAE - SDET - SDE:
    https://automationreinvented.blogspot.com/search/label/Python

    No comments:

    Post a Comment

    All Time Popular Posts

    Most Featured Post

    Introduction to Web Testing with Playwright

      Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...