{"id":4163,"date":"2021-09-09T13:13:02","date_gmt":"2021-09-09T10:13:02","guid":{"rendered":"https:\/\/sysdba.org\/?p=3787"},"modified":"2021-09-09T13:13:02","modified_gmt":"2021-09-09T10:13:02","slug":"how-to-connect-to-postgresql-database","status":"publish","type":"post","link":"https:\/\/sysdba.org\/en\/how-to-connect-to-postgresql-database\/","title":{"rendered":"How to connect to PostgreSQL Database"},"content":{"rendered":"<p>This article is a simple step-by-step guide on how to connect to PostgreSQL database server from the client machine.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3927 \" src=\"https:\/\/sysdba.org\/wp-content\/uploads\/2021\/09\/PostgreSQL-connection-to-server-1-300x73.png\" alt=\"PostgreSQL connection to server\" width=\"551\" height=\"134\" \/><\/p>\n<p>[crayon]<\/p>\n<p>mehmed@dataera ~ $ ping oel7u501<\/p>\n<p>PING oel7u501 (192.168.56.11) 56(84) bytes of data.<\/p>\n<p>64 bytes from oel7u501 (192.168.56.11): icmp_seq=1 ttl=64 time=0.477 ms<\/p>\n<p>64 bytes from oel7u501 (192.168.56.11): icmp_seq=2 ttl=64 time=0.542 ms<\/p>\n<p>[\/crayon]<\/p>\n<p>When we click <strong>Add New Server<\/strong> in <strong>PgAdmin4<\/strong>, we get the below error even though all the information is correct.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3790 size-large\" src=\"https:\/\/sysdba.org\/wp-content\/uploads\/2021\/09\/PostgreSQL-connection_1-1-1024x396.png\" alt=\"\" width=\"640\" height=\"248\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3789\" src=\"https:\/\/sysdba.org\/wp-content\/uploads\/2021\/09\/PostgreSQL-connection_2-1-232x300.png\" alt=\"\" width=\"618\" height=\"799\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>This error can be due to multiple causes:<\/p>\n<p><strong>Step 1:<\/strong><\/p>\n<p>Firewall in the PostgreSQL Linux server may be blocking the connection. By default, PostgreSQL listens on a TCP port 5432. \u00a0Linux uses firewall utility called iptables. Execute the below commands to give access to the client.<\/p>\n<p>Allow PostgreSQL.<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ iptables -A INPUT -i eth1 -p tcp -s 0.0.0.0\u00a0 &#8211;dport 5432 -j ACCEPT<\/p>\n<p>[\/crayon]<\/p>\n<p>Or to control the access only from the local subnet, we can use the below command.<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ iptables -A INPUT -i eth1 -p tcp -s 192.168.56.0\/24\u00a0 &#8211;dport 5432 -j ACCEPT<\/p>\n<p>[\/crayon]<\/p>\n<p>Make sure to check all the connections are allowed as required.<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ iptables -L<\/p>\n<p>[\/crayon]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Step 2: <\/strong><\/p>\n<p>Make sure PostgreSQL service listening correct server IP address and port. (in our case server IP is 192.168.56.11)<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ netstat -tnlp |grep 5432<\/p>\n<p>tcp\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0 0 127.0.0.1:5432\u00a0 0.0.0.0:*\u00a0\u00a0 LISTEN\u00a0\u00a0\u00a0\u00a0\u00a0 13365\/postmaster<\/p>\n<p>tcp6\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0 0 ::1:5432\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 :::*\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LISTEN\u00a0\u00a0\u00a0\u00a0\u00a0 13365\/postmaster<\/p>\n<p>[\/crayon]<\/p>\n<p>Next make changes in PostgreSQL, so that server can listen to the server IP address.<\/p>\n<p>[crayon]<\/p>\n<p>vim \/var\/lib\/pgsql\/10\/data\/postgresql.conf<\/p>\n<p>listen_addresses = &#8216;192.168.56.11&#8217;<\/p>\n<p>[\/crayon]<\/p>\n<p>Restart the PostgreSQL service to implement the above changes.<\/p>\n<p>[crayon]<\/p>\n<p>systemctl restart postgresql-10.service<\/p>\n<p>[\/crayon]<\/p>\n<p>Finally check the service is listening on the correct port with the below command<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ netstat -ntlp |grep 5432<\/p>\n<p>tcp\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 0\u00a0\u00a0\u00a0\u00a0\u00a0 0 192.168.56.11:5432\u00a0\u00a0\u00a0\u00a0\u00a0 0.0.0.0:*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 LISTEN\u00a0\u00a0\u00a0\u00a0\u00a0 15799\/postmaster<\/p>\n<p>[\/crayon]<\/p>\n<p><strong>Step 3:<\/strong><\/p>\n<p>In the PostgreSQL server, we enter IP address of the client as a trusted connection.<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~ $ vim \/var\/lib\/pgsql\/10\/data\/pg_hba.conf<\/p>\n<p>host\u00a0\u00a0\u00a0 all\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 all\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 192.168.56.0\/24\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 md5<\/p>\n<p>[\/crayon]<\/p>\n<p>With the above changes in pg_hba.conf file, we have given access to all IPs in the 192.168.56.0 subnet.<\/p>\n<p>With 192.168.56.1\/32, we can only grant access to the IP we want.<\/p>\n<p>If only one client is going to access it, it&#8217;s safer to allow access to a single IP.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 4: <\/strong><\/p>\n<p>Reloading the configuration file<\/p>\n<p>[crayon]<\/p>\n<p>postgres=# SELECT pg_reload_conf();<\/p>\n<p>pg_reload_conf<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>t<\/p>\n<p>[\/crayon]<\/p>\n<p>&nbsp;<\/p>\n<p>The connection can be established now<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3788 size-large\" src=\"https:\/\/sysdba.org\/wp-content\/uploads\/2021\/09\/PostgreSQL-connection_3-1-1024x431.png\" alt=\"\" width=\"640\" height=\"269\" \/><\/p>\n<p>Connection to server via terminal.<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u502 ~ $ psql -h 192.168.56.11 -p 5432 -d cms -U postgres -W<\/p>\n<p>Password for user postgres:<\/p>\n<p>psql (9.2.23, server 10.5)<\/p>\n<p>WARNING: psql version 9.2, server version 10.0.<\/p>\n<p>Some psql features might not work.<\/p>\n<p>Type &#8220;help&#8221; for help.<\/p>\n<p>cms=#<\/p>\n<p>[\/crayon]<\/p>\n<p>Note: It gives a warning when the client version and the server version are different.<\/p>\n<p>[crayon]<\/p>\n<p>[root@oel7u502 ~ $ \/usr\/pgsql-10\/bin\/psql -h 192.168.56.11 -p 5432 -d cms -U postgres -W<\/p>\n<p>Password for user postgres:<\/p>\n<p>psql (10.5)<\/p>\n<p>Type &#8220;help&#8221; for help.<\/p>\n<p>cms=#<\/p>\n<p>[\/crayon]<\/p>\n<p>To see the connections and processes<\/p>\n<p>[crayon]<\/p>\n<p>root@oel7u501 ~]# ps -ef |grep postgres<\/p>\n<p>postgres\u00a0 2374\u00a0\u00a0\u00a0\u00a0 1\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 \/usr\/pgsql-10\/bin\/postmaster -D \/var\/lib\/pgsql\/10\/data\/<\/p>\n<p>postgres\u00a0 2376\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: logger process<\/p>\n<p>postgres\u00a0 2378\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: checkpointer process<\/p>\n<p>postgres\u00a0 2379\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: writer process<\/p>\n<p>postgres\u00a0 2380\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: wal writer process<\/p>\n<p>postgres\u00a0 2381\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: autovacuum launcher process<\/p>\n<p>postgres\u00a0 2382\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:02 postgres: stats collector process<\/p>\n<p>postgres\u00a0 2383\u00a0 2374\u00a0 0 06:45 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: bgworker: logical replication launcher<\/p>\n<p>postgres\u00a0 6887\u00a0 2374\u00a0 0 07:58 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:29 postgres: postgres postgres 192.168.56.1(59086) idle<\/p>\n<p>postgres\u00a0 6935\u00a0 2374\u00a0 0 07:59 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: postgres cms 192.168.56.1(59522) idle<\/p>\n<p>postgres 17631\u00a0 2374\u00a0 0 10:51 ?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 00:00:00 postgres: postgres cms 192.168.56.12(38782) idle<\/p>\n<p>[\/crayon]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is a simple step-by-step guide on how to connect to PostgreSQL database server from the client machine. &nbsp; [crayon] mehmed@dataera ~ $ ping oel7u501 PING oel7u501 (192.168.56.11) 56(84) bytes of data. 64 bytes from oel7u501 (192.168.56.11): icmp_seq=1 ttl=64 time=0.477 ms 64 bytes from oel7u501 (192.168.56.11): icmp_seq=2 ttl=64 time=0.542 ms [\/crayon] When we click <a href=\"https:\/\/sysdba.org\/en\/how-to-connect-to-postgresql-database\/\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  How to connect to PostgreSQL Database<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3769,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[355],"tags":[],"class_list":["post-4163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgre-sql"],"_links":{"self":[{"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/posts\/4163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/comments?post=4163"}],"version-history":[{"count":0,"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/posts\/4163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sysdba.org\/en\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/media?parent=4163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/categories?post=4163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysdba.org\/en\/wp-json\/wp\/v2\/tags?post=4163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}