{"id":918,"date":"2023-01-12T11:28:12","date_gmt":"2023-01-12T11:28:12","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=918"},"modified":"2023-01-12T11:28:12","modified_gmt":"2023-01-12T11:28:12","slug":"install-configure-postgresql-amazon-linux","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=918","title":{"rendered":"How to Install &amp; Configure PostgreSQL on Amazon Linux"},"content":{"rendered":"<p>If you want to install PostgreSQL on Amazon Linux (AWS ec2), this guide is for you. It covers installation, configuration, and enabling remote connection.<\/p>\n<h2 id=\"install-postgresql-on-amazon-linux\">Install PostgreSQL on Amazon 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> Let&#8217;s upgrade the system Yum packages.<\/p>\n<pre><code>sudo yum update -y<\/code><\/pre>\n<p><strong>Step 2:<\/strong> PostgreSQL is part of the amazon extras library. Install the PostgreSQL amazon extras repository. At the time of writing, PostgreSQL 14 is the latest package available in the extras library.<\/p>\n<pre><code>sudo amazon-linux-extras enable postgresql14<\/code><\/pre>\n<p><strong>Step 3: <\/strong>Install the PostgreSQL server.<\/p>\n<pre><code>sudo yum install postgresql-server -y<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Initialize the DB.<\/p>\n<pre><code>sudo postgresql-setup initdb<\/code><\/pre>\n<p><strong>Step 5: <\/strong>Add the PostgreSQL service to the system startup.<\/p>\n<pre><code>sudo systemctl start postgresql\nsudo systemctl enable postgresql<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Check the status of PostgreSQL using the following command.<\/p>\n<pre><code>sudo systemctl status postgresql<\/code><\/pre>\n<h2 id=\"set-password-for-postgres-user\">Set Password For Postgres User<\/h2>\n<p>Now, let&#8217;s set a password for the default Postgres user and secure it.<\/p>\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<h2 id=\"enable-remote-connection-for-postgresql-on-amazon-linux\">Enable Remote Connection For PostgreSQL on Amazon Linux<\/h2>\n<p>By default the remote PostgreSQL connection is disabled. You need to add the following configuration to enable remote connectivity.<\/p>\n<p>Open the <code>postgresql.conf<\/code> file in the <a href=\"https:\/\/devopscube.com\/linux-vi-editor-shortcuts-beginners\/\">vi editor<\/a>.<\/p>\n<pre><code>sudo vi \/var\/lib\/pgsql\/data\/postgresql.conf<\/code><\/pre>\n<p>Locate the line that starts with &#8220;<strong>listen_addresses<\/strong>&#8220;. Uncomment and change it to &#8220;<strong>listen_addresses = &#8216;*&#8217;<\/strong>&#8220;. This will allow connections from any IP address.<\/p>\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-12-31.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"626\" height=\"372\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-12-31.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-12-31.png 626w\"><\/figure>\n<p>Next, open<code> \/var\/lib\/pgsql\/data\/pg_hba.conf<\/code> file<\/p>\n<pre><code>sudo vi \/var\/lib\/pgsql\/data\/pg_hba.conf<\/code><\/pre>\n<p>Add the following to the end of the file to allow client connections to all databases.<\/p>\n<pre><code>host    all          all            0.0.0.0\/0  md5<\/code><\/pre>\n<p>To apply all the changes, restart the PostgreSQL service using the following command.<\/p>\n<pre><code>sudo systemctl restart postgresql<\/code><\/pre>\n<p>Now, in the ec2 security group of the Amazon Linux ec2 server, allow <strong>incoming traffic on port 5432<\/strong>, which is the default port used by PostgreSQL<\/p>\n<h2 id=\"important-postgresql-server-configurations-on-amazon-linux\">Important PostgreSQL Server Configurations on Amazon Linux<\/h2>\n<p>The following table contains important PostgreSQL configurations on the Amazon Linux ec2 server.<\/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\/data\/postgresql.conf<br \/>\/var\/lib\/pgsql\/data\/<!--kg-card-begin: html--><span style=\"background-color: initial; font-family: inherit; font-size: inherit; color: var(--cs-color-primary);\">pg_hba.conf<\/span><!--kg-card-end: html--><\/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\/data<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we looked at the steps to install PostgreSQL on an Amazon Linux ec2 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 Redhat server, Checkout the <a href=\"https:\/\/devopscube.com\/install-postgresql-redhat-linux\/\" rel=\"noreferrer noopener\">PostgreSQL installation on Redhat<\/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 the 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-configure-postgresql-amazon-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install &amp;amp; Configure PostgreSQL on Amazon Linux \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/install-configure-postgresql-amazon-linux\/<\/p>\n","protected":false},"author":1,"featured_media":919,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-918","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\/918","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=918"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/918\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/919"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}