{"id":821,"date":"2021-11-20T15:53:19","date_gmt":"2021-11-20T09:53:19","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=821"},"modified":"2021-11-20T16:03:20","modified_gmt":"2021-11-20T10:03:20","slug":"how-to-allow-remote-user-access-in-postgresql","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-allow-remote-user-access-in-postgresql.html","title":{"rendered":"How to Allow Remote User Access in PostgreSQL"},"content":{"rendered":"\n<p>Configuration PostgreSQL is a bit tricky and complicated compared to MySQL for a few cases. One such case would be allowing remote user access in PGSQL. You might be interested to allow your home network to connect to your test or production database to run a few queries. To do that, if you are not using a tool like Percona Distribution for Postgres or a Cluster tool like Clustercontrol, it might get a bit complicated. <\/p>\n\n\n\n<p>I have hence, set a list of steps, we need to do to achieve the goal. <\/p>\n\n\n\n<p><strong>Set PostgreSQL to Allow Remote Addresses<\/strong><\/p>\n\n\n\n<p>To do that, open up your postgresql.conf file. It is usually located under \/var\/lib\/pgsql\/9.6\/data\/postgresql.conf file. I am running PGSQL 9.6, hence the folder is in 9.6. If you are running any other version of PGSQL, it shall change based on the version. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nano \/var\/lib\/pgsql\/9.6\/data\/postgresql.conf<\/pre>\n\n\n\n<p>Find the directive &#8216;listen_address&#8217; and uncomment it, if it is commented. Also, set this to wildcard like the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">listen_address = \"*\"<\/pre>\n\n\n\n<p><strong>Create a PostgreSQL User with Encrypted Password<\/strong><\/p>\n\n\n\n<p>Open up your psql console from the user postgres and run the query given below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">su - postgres\npsql<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create user new_user with encrypted password 'testu1234'<\/pre>\n\n\n\n<p>You should always use &#8216;with encrypted password&#8217;, as this would avoid storing your password in plaintext instead of md5.<\/p>\n\n\n\n<p>Now, grant the user on the database, you would like it to access:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">grant all privileges on database my_prod to new_user;<\/pre>\n\n\n\n<p><strong>Add Remote Access for User in PostgreSQL<\/strong><\/p>\n\n\n\n<p>PostgreSQL manages a remote access file, to allow access from remote sources. The file is located in \/var\/lib\/pgsql\/9.6\/data\/pg_hba.conf. To allow remote access from an IP, we need to add a line in this file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Edit the file in nano\n\nnano \/var\/lib\/pgsql\/9.6\/data\/pg_hba.conf\n\nAdd the following line\n\nhost    all             new_user           27.147.176.2\/32       md5<\/pre>\n\n\n\n<p>Here 27.147.176.2 is the IP from where I would like to access the database.<\/p>\n\n\n\n<p><strong>Restart PostgreSQL<\/strong><\/p>\n\n\n\n<p>Now, restart your PostgreSQL instance<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">systemctl restart postgresql-9.6.service<\/pre>\n\n\n\n<p>You should be now set. <\/p>\n\n\n\n<p><strong>Few Additional Things For Reference<\/strong><\/p>\n\n\n\n<ol><li>How to list the users of PostgreSQL from the console?<br><br>From the Postgres console (psql), run the following command:<br>\\du<br><\/li><li>How to change the password for a PostgreSQL user?<\/li><\/ol>\n\n\n\n<p>From the Postgres console (psql), run the following query:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ALTER USER username WITH ENCRYPTED PASSWORD 'password';<\/pre>\n\n\n\n<p>where username is the username you would like the password changed. Remember to use encrypted password keyword to save the password in md5.<\/p>\n\n\n\n<p>3. How to change the user to a superuser?<\/p>\n\n\n\n<p>You may use the following query:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ALTER USER new_user WITH SUPERUSER;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Configuration PostgreSQL is a bit tricky and complicated compared to MySQL for a few cases. One such case would be allowing remote user access in PGSQL. You might be interested to allow your home network to connect to your test or production database to run a few queries. To do that, if you are not &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-allow-remote-user-access-in-postgresql.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Allow Remote User Access in PostgreSQL&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[412,523],"tags":[524,614,615,616],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/821"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":2,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":823,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/821\/revisions\/823"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}