Skip to main content

SSL configuration in PostgreSQL for PgAdmin



SSL configuration in PostgreSQL for PgAdmin
This document describes how to set up SSL to enable ssl connections from PgAdmin on some client machine to postgresql on a server machine. In my example, I am using Centos 7 for PostgreSql-9.5 server and Windows for PgAdmin.The assumption is that Postgresql (compiled with ssl support) and openssl are already installed on the server (Linux Centos7). PgAdmin is already installed on the client (either Windows or Linux).
On the server, three certificates are required in the data directory.
root.crt (trusted root certificate)
server.crt (server certificate)
server.key (private key) 
On CentOS7 default Data directory is /var/lib/pgsql/9.5/data/:
Now, connect your system with user root.

Step 1
Su –
Go to data directory
[root@localhost ~]# cd /var/lib/pgsql/9.5/data/

Out put
[root@ localhost data]#

Step 2 :
Generate a private key (you must provide a passphrase).
[root@ localhost data]#openssl genrsa -des3 -out server.key 1024
Output of above command is :
Generating RSA private key, 1024 bit long modulus
......................++++++
..........++++++
e is 65537 (0x10001)
Enter pass phrase for server.key: <write any text>     [Here, I wrote prakash]
Verifying - Enter pass phrase for server.key:prakash [Here, I wrote prakash]
[root@ localhost data]#
You can see one file has created in data directory with name server.key
Step 3:
Remove the passphrase.
[root@ localhost data]#openssl rsa -in server.key -out server.key
Output
Enter pass phrase for server.key:prakash                  [Here, You’ll write prakash]
Output
writing RSA key
[root@ localhost data]#
Step 4:
Set appropriate permission and owner on the private key file.
[root@ localhost data]#chmod 400 server.key
[root@ localhost data]#chown postgres.postgres server.key
[root@ localhost data]#
Step 5:
Create the server certificate.
-subj is a shortcut to avoid prompting for the info.
-x509 produces a self signed certificate rather than a certificate request.
[root@ localhost data]#
openssl req -new -key server.key -days 3650 -out server.crt -x509 -subj '/C=CA/ST=India pune/L=Comox/O=The India.ca/CN=192.168.x.x/emailAddress=xyz1@epps.com'
[Hint 192.168.x.x -machine ip address, Change your IP address in place of 192.168.x.x]
Output
[root@ localhost data]#
You can see one file has created in data directory with name server.crt
Now, We have two files server.key and server.crt.

Step 6:
Since you are self-signing, you can use the server certificate as the trusted root certificate.
[root@ localhost data]#cp server.crt root.crt
Step 7:  
You need to edit pg_hba.conf file.  Which is located in Data Directory.
For example:
Comment all lines and Add below lines in pg_hba.conf file
[root@ localhost data]# vi pg_hba.conf

local                             all        all                                     md5

hostssl                         all        postgres   0.0.0.0/0          md5         clientcert=1
Step 8:  
You need to edit postgresql.conf to actually activate ssl:
[root@ localhost data]# vi postgresql.conf

listen_addresses = '*'

ssl = on

ssl_ca_file = 'root.crt' 
[Hint:If we configure hostssl all    postgres    0.0.0.0/0      md5      clientcert=1]
Save and exit

Step 9: 
Postgresql server must be restarted.
[root@ localhost data]# systemctl restart postgresql-9.5.service
And reboot the system
[root@ localhost data]# reboot

Step 10 :
Copy these three files, you have created on the server in /var/lib/pgsql/9.5/data/ directory to the client machine.
Eg. You can copy these file in D:\SSL directory or anywhere you want through Putty or any tools. Like below:
Suppose your pscp.exe is on D:\ Putty directory. Then on Window you open cmd and go to location of Putty.
C:\>cd d:\putty
D:\Putty> pscp.exe root@192.168.x.x:/var/lib/pgsql/9.5/data/server.crt D:\SSL\
                 root@ 192.168.x.x's password:<root password>
D:\Putty> pscp.exe root@192.168.x.x:/var/lib/pgsql/9.5/data/root.crt D:\SSL\
                 root@ 192.168.x.x's password:<root password>
D:\Putty> pscp.exe root@192.168.x.x:/var/lib/pgsql/9.5/data/server.key D:\SSL\
                 root@ 192.168.x.x's password:<root password>
Step 11 : 
Open PgAdmin
Go to File menu Click on Add Server, this displays the New Server Registration dialog box as shown below.




Input As per your configuration like below:




                    


Click on SSL menu and select your files as given below:

       

Now, click on OK.


Comments

  1. Well explained, Great work... keep posting..

    ReplyDelete
  2. very Nicely and easily explained, even new bees can implement by reading this. Thanks..keep posting

    ReplyDelete
  3. Well explained, helped to grasp easily.

    ReplyDelete
  4. Great Work sir.. It is easy to understand with step by step

    ReplyDelete
  5. Well explained, documented and cleared.

    I m expecting more blogs which help to improve my competencies.

    ReplyDelete
  6. very informative sirji!!!
    keep the good work.
    thank you.

    ReplyDelete
  7. Well Explained , Very helpful to understand About SSL Configuration for Postgres ....

    ReplyDelete
  8. Easy explanation...Thank you very much for writing about SSL Configuration for Postgres....

    ReplyDelete
  9. Great article sir. Thank you.
    I launched new portal https://StoodQ.com. This portal helps developer to join and get chance to win amazon gift cards every month by posting questions, giving answers, posting articles, blogs and comments. Easy way to create strong online presence for developers profile and get paid.

    ReplyDelete
  10. It's very easy to learn..such a great article. Thanks Sir.

    ReplyDelete
  11. I feel this is the best way of providing some useful and more aspects of SSIS in order to make it more usable for the IT world.

    SSIS PostgreSql Write

    ReplyDelete

Post a Comment

Popular posts from this blog

Partition and Subpartition in PostgreSQL12

Partitioning refers to splitting what is logically one large table into smaller physical pieces. Partitioning can provide several benefits: • Query performance can be improved dramatically in certain situations, particularly when most of the heavily accessed rows of the table are in a single partition or a small number of partitions.  • When queries or updates access a large percentage of a single partition, performance can be improved by taking advantage of sequential scan of that partition instead of using an index and random access reads scattered across the whole table. • Bulk loads and deletes can be accomplished by adding or removing partitions, if that requirement is planned into the partitioning design. Doing ALTER TABLE DETACH PARTITION or dropping an individual partition using DROP TABLE is far faster than a bulk operation. These commands also entirely avoid the VACUUM overhead caused by a bulk DELETE. • Seldom-used data can be migrated to cheaper and slower...

PostgreSQL Foreign Data Wrapper for Oracle on Windows OS

The foreign data wrapper is responsible for fetching data from the remote data source and returning it to the PostgreSQL executor. If wrapper must support in updating foreign tables. In this post, I'll demonstrate, how you may connect PostgreSQL database to Oracle database using oracle_fdw . Before going ahead kindly ensure that PostgreSQL is installed in your system . Let’s begin: Step-1 Software’s used in demo - Windows, PostgreSQL 9.5, 9.6   instantclient-basic-win-x86-64-11.2.0.1.0 instantclient-jdbc-win-x86-64-11.2.0.1.0   instantclient-odbc-win-x86-64-11.2.0.1.0 instantclient-sqlplus-win-x86-64-11.2.0.1.0 & Oracle_FDW as per version ( http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html   , https://pgxn.org/dist/oracle_fdw/ or version wise)   Download all the files mentioned above, in a single folder and unzip entire files to this single folder. Suppose, we create a folder Oracle in c: drive ...