{"id":866,"date":"2023-01-11T15:28:24","date_gmt":"2023-01-11T15:28:24","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=866"},"modified":"2023-01-11T15:28:24","modified_gmt":"2023-01-11T15:28:24","slug":"install-postgresql-redhat-linux","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=866","title":{"rendered":"How to Install &#038; Configure PostgreSQL on Redhat Linux"},"content":{"rendered":"<p>If you want to install PostgreSQL on Redhat Linux, this guide is for you. It covers installation, configuration, and enabling remote connection.<\/p>\n<h2 id=\"install-postgresql-on-redhat-linux\">Install PostgreSQL on Redhat Linux<\/h2>\n<p>Follow the steps given below to install the latest version of PostgreSQL on RedHat Linux<\/p>\n<p><strong>Step 1:<\/strong> Add  the PostgreSQL Yum Repo<\/p>\n<p>Execute the following command to add the PostgreSQL yum repo.<\/p>\n<pre><code>sudo yum install -y https:\/\/download.postgresql.org\/pub\/repos\/yum\/reporpms\/EL-7-x86_64\/pgdg-redhat-repo-latest.noarch.rpm<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Install the latest version of the PostgreSQL Server<\/p>\n<p>The latest version at the time of writing this guide is PostgreSQL 15. You can check the latest version available for Rehat on <a href=\"https:\/\/www.postgresql.org\/download\/linux\/redhat\/?ref=devopscube.com\" rel=\"noreferrer noopener\">this page<\/a>.<\/p>\n<pre><code>sudo yum install -y postgresql15-server<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Initialize the Database and Enable Automatic Start<\/p>\n<p>Execute the following commands to initialize the PostgreSQL database and add services to the server startup.<\/p>\n<pre><code>sudo \/usr\/pgsql-15\/bin\/postgresql-15-setup initdb\nsudo systemctl enable postgresql-15\nsudo systemctl start postgresql-15<\/code><\/pre>\n<h2 id=\"enable-postgresql-remote-connection\">Enable PostgreSQL Remote Connection<\/h2>\n<p>By default the remote PostgreSQL connection is disabled. However, you can connect to the database locally. If you try to connect from a remote machine you will get an error like the following.<\/p>\n<pre><code>psql: error: connection to server at \"34.221.35.108\", port 5432 failed: Connection refused\n\tIs the server running on that host and accepting TCP\/IP connections?<\/code><\/pre>\n<p>First login to the database using the following command.<\/p>\n<pre><code>sudo -u postgres psql<\/code><\/pre>\n<p>Set the password for the Postgres user so that we can use it to log in remotely. Replace <code>myPassword<\/code> with the required password.<\/p>\n<pre><code>ALTER USER postgres PASSWORD 'myPassword';<\/code><\/pre>\n<p>To enable PostgreSQL remote connection on Redhat, you need to open the following file.<\/p>\n<pre><code>sudo vi \/var\/lib\/pgsql\/15\/data\/postgresql.conf<\/code><\/pre>\n<p>Under <code>CONNECTIONS AND AUTHENTICATION<\/code> section you will find the following commented parameter.<\/p>\n<pre><code>#listen_addresses = 'localhost' <\/code><\/pre>\n<p>Replace it with the following and save the file. With this configuration, we are enabling PostgreSQL server connections to accept connections from all IP addresses.<\/p>\n<pre><code>listen_addresses = '*' <\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-22-32.png\" class=\"kg-image\" alt=\"PostgreSQL listen_addresses parameter\" loading=\"lazy\" width=\"614\" height=\"365\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-22-32.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-22-32.png 614w\"><\/figure>\n<p>To apply the change, restart the PostgreSQL service using the following command. If you are using any version other than 15, replace 15 with the relevant version number in the command.<\/p>\n<pre><code>sudo systemctl restart postgresql-15<\/code><\/pre>\n<p>We have allowed connections on all interfaces. However, If you try to connect to the PostgreSQL RedHat server from a remote machine, you will get the following error because you need to enable client connections to all the Databases and users.<\/p>\n<pre><code>psql: error: connection to server at \"34.221.35.108\", port 5432 failed: FATAL:  no pg_hba.conf entry for host \"49.205.132.96\", user \"postgres\", database \"postgres\", no encryption<\/code><\/pre>\n<p>To allow client connections to all databases, you need to edit the <strong>pg_hba.conf<\/strong> file.<\/p>\n<p>Open <strong>pg_hba.conf<\/strong> file.<\/p>\n<pre><code>sudo vi \/var\/lib\/pgsql\/15\/data\/pg_hba.conf <\/code><\/pre>\n<p>Add the following line to the <strong>end of the file<\/strong>.<\/p>\n<pre><code>host    all          all            0.0.0.0\/0  md5<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-10-45.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"573\" height=\"316\"><\/figure>\n<p>Restart the PostgreSQL server to apply changes.<\/p>\n<pre><code>sudo systemctl restart postgresql-15<\/code><\/pre>\n<p>Now, validate the remote connection by connecting to the PostgreSQL server using the following command. Replace IP with your server IP.<\/p>\n<pre><code>psql -h 34.221.35.108 -U postgres<\/code><\/pre>\n<p>When prompted for the password, use the password you set using the ALTER command.<\/p>\n<p>If you don&#8217;t have a password set for the Postgres user, you will get the following error.<\/p>\n<pre><code>Password for user postgres:\npsql: error: connection to server at \"34.221.35.108\", port 5432 failed: FATAL:  password authentication failed for user \"postgres\"<\/code><\/pre>\n<h2 id=\"important-postgresql-server-configurations-on-redhat\">Important PostgreSQL Server Configurations on Redhat<\/h2>\n<p>The following table contains important PostgreSQL configurations on the Redhat server. Replace 15 with the version of PostgreSQL you are using.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table>\n<thead>\n<tr>\n<th>Config<\/th>\n<th>Details<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>PostgreSQL default port<\/td>\n<td>5432<\/td>\n<\/tr>\n<tr>\n<td>Default user<\/td>\n<td>postgres<\/td>\n<\/tr>\n<tr>\n<td>Config files location (<code>postgresql.conf &amp; pg_hba.conf<\/code>&nbsp;)<\/td>\n<td>\/var\/lib\/pgsql\/15\/data\/pg_hba.conf <br \/>\/var\/lib\/pgsql\/15\/data\/postgresql.conf <\/td>\n<\/tr>\n<tr>\n<td>Default database<\/td>\n<td>postgres<\/td>\n<\/tr>\n<tr>\n<td>Default data directory<\/td>\n<td>\/var\/lib\/pgsql\/15\/data<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As a DevOps engineer, it is essential to know the basic configurations involved in a Database. in this guide we looked at the steps to install PostgreSQL on a Redhat server.<\/p>\n<p>For the Ubuntu server, checkout the <a href=\"https:\/\/devopscube.com\/install-postgresql-on-ubuntu\/\" rel=\"noreferrer noopener\">PostgreSQL installation on Ubuntu<\/a>.<\/p>\n<p>For the Amazon Linux server, Checkout the <a href=\"https:\/\/devopscube.com\/install-configure-postgresql-amazon-linux\/\" rel=\"noreferrer noopener\">PostgreSQL installation on Amazon Linux<\/a><\/p>\n<p>Also, if you are using <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\">Kubernetes<\/a>, check out the guide on <a href=\"https:\/\/devopscube.com\/deploy-postgresql-statefulset\/\">setting up PostgreSQL statefulset on Kubernetes<\/a><\/p>\n<p>If you are starting your DevOps engineer journey, look at my comprehensive <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">guide to becoming a DevOps engineer<\/a>.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/install-postgresql-redhat-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install &amp; Configure PostgreSQL on Redhat Linux \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/install-postgresql-redhat-linux\/<\/p>\n","protected":false},"author":1,"featured_media":867,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-866","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=866"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/866\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/867"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}