'connect and Run postgres query in Jenkins shell

I have installed postgres as part of Docker image and it seems postgres is installed. I have tested my docker file named "test2" on my local and it seems postgresql is installed. I will call this docker file in my jenkins agent, requirement is:-

How to connect this postgresl and pass querys in file using jenkins shell.

Below is jenkins code '''

 - agent {
           docker {
               image 'sdartifact.td.abc.com:7003/service/test2'
           }
       }    stages {
           stage("POSTGRES sql connectivity") {
   
                   
                         sh """
                         // code to connect to postgres sql and pass query in txt file
                         """
   
   
                       } }

'''



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source