{"id":540,"date":"2023-01-12T03:49:00","date_gmt":"2023-01-12T03:49:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=540"},"modified":"2023-01-12T03:49:00","modified_gmt":"2023-01-12T03:49:00","slug":"install-postgresql-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=540","title":{"rendered":"How to Install PostgreSQL on Ubuntu [Setup &#038; Configurations]"},"content":{"rendered":"<p>This guide will teach you how to <strong>install PostgreSQL on Ubuntu<\/strong> and change its key configurations.<\/p>\n<p><a href=\"https:\/\/www.postgresql.org\/?ref=devopscube.com\" rel=\"noreferrer noopener\">PostgreSQL<\/a> is one of the best open source <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object%E2%80%93relational_database?ref=devopscube.com\" rel=\"noreferrer noopener\">object-relational database systems<\/a>. Whether you are a developer, sysadmin, or DevOps engineer, it is essential to know the basic setup and configurations involved in a PostgreSQL setup.<\/p>\n<p>As per the latest StackOverflow developer survey, <a href=\"https:\/\/insights.stackoverflow.com\/survey\/2021?ref=devopscube.com\" rel=\"noreferrer noopener\">40.42% of developers use PostgreSQL<\/a> as a database.<\/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-25-28.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"540\" height=\"505\"><\/figure>\n<h2 id=\"install-postgresql-on-ubuntu\">Install PostgreSQL on Ubuntu<\/h2>\n<p>It is always better to download the latest version available in the official PostgreSQL Ubuntu repository.<\/p>\n<pre><code>\nsudo sh -c 'echo \"deb http:\/\/apt.postgresql.org\/pub\/repos\/apt $(lsb_release -cs)-pgdg main\" &gt; \/etc\/apt\/sources.list.d\/pgdg.list'\n\nwget --quiet -O - https:\/\/www.postgresql.org\/media\/keys\/ACCC4CF8.asc | sudo apt-key add -\n\nsudo apt-get update<\/code><\/pre>\n<p>Install the latest version of PostgreSQL<\/p>\n<pre><code>sudo apt-get -y install postgresql<\/code><\/pre>\n<p>If you want to install a specific version of PostgreSQL, you can search for the available versions <a href=\"https:\/\/www.postgresql.org\/download\/linux\/ubuntu\/?ref=devopscube.com\" rel=\"noreferrer noopener\">from here<\/a> and use the version with the install command as shown below.<\/p>\n<pre><code>sudo apt-get -y install postgresql-17<\/code><\/pre>\n<p>Lets validate PostgreSQL by checking the service status.<\/p>\n<pre><code>sudo systemctl status postgresql<\/code><\/pre>\n<h2 id=\"connect-to-postgresql-change-password\">Connect to PostgreSQL &amp; Change Password<\/h2>\n<p>You can connect to the PostgreSQL server using the <code>psql<\/code> client<\/p>\n<p>When you install PostgreSQL, the <code>psql<\/code> client also gets installed.<\/p>\n<p>Execute the following command to connect to the server.<\/p>\n<pre><code>sudo -u postgres psql<\/code><\/pre>\n<p>Let&#8217;s check the version of PostgreSQL using the following command.<\/p>\n<pre><code>SELECT version();<\/code><\/pre>\n<p>Now, we have to change the <code>postgres<\/code> user&#8217;s password so that we can <strong>allow remote PostgreSQL connection<\/strong> using password authentication.<\/p>\n<p>Execute the following query to set a password. Replace myPassword with a password of your choice.<\/p>\n<pre><code>ALTER USER postgres PASSWORD 'myPassword';<\/code><\/pre>\n<h2 id=\"postgresql-remote-connection-configuration\">PostgreSQL Remote Connection Configuration<\/h2>\n<p>By default, the PostgreSQL server is accessible only on the loopback interface (127.0.0.1) on port 5432 and through the Unix socket. Therefore, you can only connect to the PostgreSQL server from within the Ubuntu server where PostgreSQL is installed .<\/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-26-24.png\" class=\"kg-image\" alt=\"PostgreSQL remote connection architecture.\" loading=\"lazy\" width=\"613\" height=\"491\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-26-24.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-26-24.png 613w\"><\/figure>\n<p>You can verify it by listing all the TCP connections using the <code>ss<\/code> <a href=\"https:\/\/devopscube.com\/list-linux-networking-troubleshooting-and-commands-beginners\/\">Linux networking command.<\/a><\/p>\n<pre><code>ss -nlt<\/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-20-35.png\" class=\"kg-image\" alt=\"PostgreSQL listening on loopback interface (127.0.0.1)\" loading=\"lazy\" width=\"598\" height=\"131\"><\/figure>\n<p>To enable remote connection to PostgreSQL, we need to allow it in the postgresql.conf file.<br \/>For Ubuntu systems, <code>postgresql.conf <\/code>is located in <code>\/etc\/postgresql\/17\/main\/<\/code> location. 17 is the version number.<\/p>\n<p>If you are using a different PostgreSQL version, change it accordingly.<\/p>\n<p>Or you can use the following find command to locate the file.<\/p>\n<pre><code>sudo find \/ -name \"postgresql.conf\"<\/code><\/pre>\n<p>Open the file <code>postgresql.conf<\/code> file.<\/p>\n<pre><code>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<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-30.png\" class=\"kg-image\" alt=\"PostgreSQL listen_addresses parameter\" loading=\"lazy\" width=\"619\" height=\"368\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-22-30.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-22-30.png 619w\"><\/figure>\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<p>To apply the change, restart the PostgreSQL service.<\/p>\n<pre><code>sudo systemctl restart postgresql<\/code><\/pre>\n<p>Now, if you list all the TCP connections, you can see the PostgreSQL service listening on all interfaces, as shown below.<\/p>\n<pre><code>ss -nlt<\/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-21-35.png\" class=\"kg-image\" alt=\"PostgreSQL listening on all interfaces\" loading=\"lazy\" width=\"597\" height=\"158\"><\/figure>\n<p>We have allowed only connections on all interfaces. However, If you try to connect to PostgreSQL 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 \"\", port 5432 failed: FATAL:  no pg_hba.conf entry for host \"\", user \"postgres\", database \"postgres\", SSL encryption\nconnection to server at \"\", port 5432 failed: FATAL:  no pg_hba.conf entry for host \"\", 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 \/etc\/postgresql\/17\/main\/pg_hba.conf <\/code><\/pre>\n<p>Add the following line to the end of the file.<\/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-23-34.png\" class=\"kg-image\" alt=\"PostgreSQL pg_hba.conf configuration\" loading=\"lazy\" width=\"629\" height=\"347\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-23-34.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-23-34.png 629w\"><\/figure>\n<p>To apply the change, restart the PostgreSQL service.<\/p>\n<pre><code>sudo systemctl restart postgresql<\/code><\/pre>\n<h2 id=\"test-postgresql-remote-connection\">Test PostgreSQL Remote Connection<\/h2>\n<p>Now, let&#8217;s try connecting PostgreSQL from a remote machine.<\/p>\n<p>First, install the PostgreSQL client on the system you are trying to connect to the PostgreSQL server.<\/p>\n<p>For Ubuntu,<\/p>\n<pre><code>sudo apt-get install postgresql-client<\/code><\/pre>\n<p>For MAC<\/p>\n<pre><code>brew install libpq\nbrew link --force libpq<\/code><\/pre>\n<p>Once the client is installed, execute the following psql command to connect to the PostgreSQL remotely. Replace 192.168.5.5 with your PostgreSQL server IP. When it prompts for a password, enter the password we generated during the initial configuration.<\/p>\n<pre><code>psql -h 192.168.5.5 -U postgres<\/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-24-28.png\" class=\"kg-image\" alt=\"PostgreSQL remote login\" loading=\"lazy\" width=\"599\" height=\"334\"><\/figure>\n<h2 id=\"important-postgresql-server-configurations\">Important PostgreSQL Server Configurations<\/h2>\n<p>The following table contains important PostgreSQL configurations.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table class=\"has-fixed-layout\">\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> )<\/td>\n<td>\/etc\/postgresql\/postgresql.conf<br \/>\/etc\/postgresql\/pg_hba.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\/postgresql\/<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"postgresql-server-installation-faqs\">PostgreSQL Server Installation FAQs<\/h2>\n<p>The following are the frequently asked questions about PostgreSQL Server Installation.<\/p>\n<h3 id=\"does-postgresql-have-a-default-database\">Does PostgreSQL have a default database?<\/h3>\n<p>Yes. When you install PostgreSQL, a default database named Postgres gets created. It contains user information, utilities, and third-party applications.<\/p>\n<h3 id=\"what-is-the-location-of-postgresqlconf-file\">What is the location of postgresql.conf file?<\/h3>\n<p>The <code>postgresql.conf<\/code> file is located at the <code>\/etc\/postgresql\/<\/code> location. For example, if you are using PostgreSQL 17, the absolute url of <code>postgresql.conf<\/code> will be <code>\/etc\/postgresql\/17\/main\/postgresql.conf<\/code>.<\/p>\n<h3 id=\"what-is-the-location-of-pghbaconf-file\">What is the location of pg_hba.conf file?<\/h3>\n<p>The <code>pg_hba.conf<\/code> file is located at the <code>\/etc\/postgresql\/ <\/code>location. For example, if you are using PostgreSQL 17, the absolute url of <code>pg_hba.conf<\/code> would be <code>\/etc\/postgresql\/17\/main\/pg_hba.conf <\/code>.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>This guide looked at the steps to install PostgreSQL on a Ubuntu server.<\/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>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 my guide on <a href=\"https:\/\/devopscube.com\/deploy-postgresql-statefulset\/\">setting up PostgreSQL statefulset on Kubernetes<\/a><\/p>\n<p>As a DevOps engineer, it is essential to know the basic configurations involved in a Database.<\/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-on-ubuntu\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install PostgreSQL on Ubuntu [Setup &amp; Configurations] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/install-postgresql-on-ubuntu\/<\/p>\n","protected":false},"author":1,"featured_media":541,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-540","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\/540","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=540"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/541"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}