{"id":15945,"date":"2019-03-14T07:51:25","date_gmt":"2019-03-14T07:51:25","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15945"},"modified":"2025-09-10T15:32:06","modified_gmt":"2025-09-10T15:32:06","slug":"how-to-install-postgresql-on-ubuntu","status":"publish","type":"post","link":"\/tutorials\/how-to-install-postgresql-on-ubuntu","title":{"rendered":"How to install PostgreSQL on Ubuntu and usage instructions for effective database management"},"content":{"rendered":"<p>PostgreSQL is a popular open-source relational database management system (RDBMS) known for its scalability and efficiency in handling large amounts of data.<\/p><p>Due to its high performance, PostgreSQL is ideal for large enterprise websites or applications. It also supports various data types and programming languages, including PHP and Python.<\/p><p>In this tutorial, we will explain how to install PostgreSQL on<strong> Ubuntu 22.04 <\/strong>and later. We will also cover the pgAdmin installation process and solutions to common PostgreSQL issues.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/public\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n<div class=\"protip\">\n                    <h2 class=\"featured-snippet title\">What Is PostgreSQL?<\/h2>\n                    <p> PostgreSQL is a relational database management system (RDBMS) popular for its performance. Unlike a standard DBMS, it arranges data in a logical structure for more efficient access.<br>\nHighly efficient and capable of handling many queries, the database solution is excellent for large enterprises. Moreover, it is free, open-source, and compatible with various data types.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-postgresql-on-ubuntu\">How to Install PostgreSQL on Ubuntu<\/h2><p>Before proceeding, ensure your virtual private server (VPS) runs <strong><a href=\"\/vps\/ubuntu-hosting\">Ubuntu 22.04<\/a> <\/strong>or later. Otherwise, follow our guide to <a href=\"https:\/\/support.hostinger.com\/en\/articles\/4965922-how-to-change-the-operating-system-for-your-vps\" target=\"_blank\" rel=\"noopener\">change the operating system in Hostinger&rsquo;s VPS<\/a>.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-install-postgresql\">1. Install PostgreSQL<\/h3><p>PostgreSQL installation involves executing <a href=\"\/tutorials\/linux-commands\">Linux commands<\/a> on your machine. For a local system, run them directly via Terminal, the Linux command prompt.<\/p><p>For a remote machine like VPS, connect <a href=\"\/tutorials\/how-to-use-putty-ssh\">using an SSH application<\/a> or Terminal. Hostinger users can find their remote machine login credentials by navigating to the <strong>VPS overview<\/strong> menu<strong> <\/strong>in hPanel.<\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df8f8c16a13\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-full wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"1041\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/public\" alt=\"The SSH access credentials on the VPS overview menu\" class=\"wp-image-125811\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2021\/10\/vps-ssh-username-and-ip-address-on-hpanel.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>You can install PostgreSQL Ubuntu packages from the local repository or using the APT package manager. We recommend the latter to ensure you get the latest version.<\/p><p>Our VPS also supports other databases. For example, you can <a href=\"\/tutorials\/how-to-install-mongodb-on-ubuntu\/\">install MongoDB<\/a> if you prefer a NoSQL DBMS. <\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p><strong>Option 1 &ndash; Installation From APT Repository<\/strong><\/p><p>The popular method to install PostgreSQL on Ubuntu is from the official repository via the <strong>APT package manager<\/strong>. Here are the steps to do so:<\/p><ol class=\"wp-block-list\">\n<li>Run these commands to import the key from PostgreSQL&rsquo;s official repository to the APT package manager:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo 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 gpg -<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Refresh APT to synchronize all repositories using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt update<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Install the latest version of PostgreSQL with APT and sudo privileges:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get -y install postgresql<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>To confirm the installation, run this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u postgres psql<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Enter the PostgreSQL version query statement:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">SELECT version();<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>To install a specific version, add the number in your command, for example <strong>sudo apt-get -y install postgresql-12<\/strong>. In this example, the command will install PostgreSQL version 12.<\/p>\n                <\/div>\n\n\n\n<\/p><p><strong>Option 2 &ndash; Installation Using Local Ubuntu Repository<\/strong><\/p><p>The alternative PostgreSQL installation method uses the local <a href=\"\/tutorials\/what-is-ubuntu\">Ubuntu<\/a> repository. Here are the steps:<\/p><ol class=\"wp-block-list\">\n<li>Run this command to refresh the Ubuntu repository:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get update<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Verify the available <strong>PostgreSQL<\/strong> <strong>apt<\/strong> <strong>repository<\/strong> using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt show postgresql<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Run this command to install the latest available version. <\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt install postgresql postgresql-contrib<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>To verify whether the PostgreSQL installation is successful, run the following:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u postgres psql<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Then, run this command to check the current PostgreSQL version:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">SELECT version();<\/pre><p>The command line should show that the <strong>PostgreSQL server version <\/strong>14.11 is successfully installed, similar to this:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\"><img decoding=\"async\" width=\"2470\" height=\"306\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/public\" alt=\"PostgreSQL version 14.11 in code\" class=\"wp-image-108576\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=2470,fit=scale-down 2470w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/PostgreSQL.png\/w=2048,fit=scale-down 2048w\" sizes=\"(max-width: 2470px) 100vw, 2470px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-2-check-the-postgresql-status\">2. Check the PostgreSQL Status<\/h3><p>After installing PostgreSQL, check whether it is enabled and active. Use the <strong>systemctl <\/strong>or <strong>service <\/strong>command utility to verify the service status:<\/p><pre class=\"wp-block-preformatted\">sudo service postgresql status\n\nsudo systemctl status postgresql<\/pre><p>If PostgreSQL is enabled, your command line will output <strong>active <\/strong>and <strong>loaded<\/strong>.<\/p><p>For Hostinger users, you can also check whether PostgreSQL is running by asking the <a href=\"https:\/\/www.hostinger.com\/blog\/kodee\">Kodee AI assistant<\/a>. Simply enter, &ldquo;Check the postgresql service status in my VPS,&rdquo; and Kodee will output the service status. <\/p><div class=\"wp-block-image\">\n<figure data-wp-context='{\"imageId\":\"69df8f8c194b6\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1460\" height=\"489\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/public\" alt=\"Kodee outputs the postgresql service status\" class=\"wp-image-124640\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-outputs-the-postgresql-service-status.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Ask Kodee to manage services<\/h4>\n                    <p>Aside from checking service statuses on your VPS, you can ask <strong>Kodee<\/strong> to reset services to their default settings and states.<\/p>\n                <\/div>\n\n\n\n<\/p><p>In addition to the service status, check whether PostgreSQL is ready to accept a connection using this command:<\/p><pre class=\"wp-block-preformatted\">sudo pg_isready<\/pre><h3 class=\"wp-block-heading\" id=\"h-3-connect-to-postgresql\">3. Connect to PostgreSQL<\/h3><p>During the installation, PostgreSQL automatically creates the default <strong>postgres <\/strong>role for logging in to the database. To switch to this user, run the following command:<\/p><pre class=\"wp-block-preformatted\">sudo su - postgres<\/pre><p>Your command line should now begin with <strong>postgres<\/strong>. To connect to the PostgreSQL database, use the PostgreSQL shell using the following:<\/p><pre class=\"wp-block-preformatted\">sudo psql<\/pre><p>The <strong>psql<\/strong> command will create a session in the default<strong> <\/strong>database. If the command isn&rsquo;t found, manually install the PostgreSQL client using this command:<\/p><pre class=\"wp-block-preformatted\">sudo apt-get install postgresql-client<\/pre><p>To check the connection status, run this command using the PostgreSQL prompt:<\/p><pre class=\"wp-block-preformatted\">\\conninfo<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\"><img decoding=\"async\" width=\"958\" height=\"72\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\/public\" alt=\"The conninfo command outputs a confirmation message on Terminal\" class=\"wp-image-93042\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\/w=958,fit=scale-down 958w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-conninfo-command-outputs-a-confirmation-message-on-Terminal.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 958px) 100vw, 958px\" \/><\/a><\/figure><\/div><p>To quit the PostgreSQL prompt and return to the regular system user, use the <strong>exit <\/strong>command. Meanwhile, use <strong>\\q <\/strong>to detach from the database&rsquo;s interactive session.<\/p><h3 class=\"wp-block-heading\" id=\"h-4-change-user-password\">4. Change User Password<\/h3><p>Changing the<strong> <\/strong>default <strong>postgres<\/strong> role password is essential to improve database security. Here&rsquo;s how to do it:<\/p><ol class=\"wp-block-list\">\n<li>Activate the PostgreSQL prompt using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u postgres psql<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Run this statement to change the <strong>postgres<\/strong> account password. Don&rsquo;t forget to replace <strong>NewPassword<\/strong> with your actual password:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">ALTER USER postgres PASSWORD 'NewPassword';<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Check whether the change is applied successfully using the following command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">\\p<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Restart the PostgreSQL service using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo service postgresql restart<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\"><img decoding=\"async\" width=\"1024\" height=\"155\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\/public\" alt=\"Postgres returns a confirmation about the account password change\" class=\"wp-image-93043\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Postgres-returns-a-confirmation-about-the-account-password-change.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-5-create-a-user-on-postgresql\">5. Create a User on PostgreSQL<\/h3><p>To manage database access and ensure proper data organization, you will need multiple PostgreSQL users. The easiest way to create one is to use the <strong>interactive mode<\/strong>. Here are the steps:<\/p><ol class=\"wp-block-list\">\n<li>Switch to the <strong>postgres<\/strong> user:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo su - postgres<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Run this command to create a new user:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">createuser --interactive<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Enter the new PostgresQL account name. For this tutorial, it is <strong>TheNewUser<\/strong>.<\/li>\n\n\n\n<li>To validate the new role, log in to PostgreSQL with the <strong>postgres<\/strong> account:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u postgres psql<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Check all the users with the following command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">\\du<\/pre><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> As a part of security best practices for PostgreSQL, avoid granting the new role a superuser privilege unless necessary.<\/p><\/div>\n\n\n\n<\/p><p>As shown in the output, the database successfully creates <strong>TheNewUser<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\"><img decoding=\"async\" width=\"1024\" height=\"189\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\/public\" alt=\"Terminal shows the new PostgreSQL user\" class=\"wp-image-93044\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-shows-the-new-PostgreSQL-user.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-6-create-a-database-on-postgresql\">6. Create a Database on PostgreSQL<\/h3><p>PostgreSQL assumes the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/user-manag.html\" target=\"_blank\" rel=\"noopener\">role<\/a> and the database will have the same name. Therefore, if you create a <strong>TheNewUser<\/strong> role, PostgreSQL will access a database with the same<strong> <\/strong>name.<\/p><p>\n\n\n<div><p class=\"important\"><strong>Important!<\/strong> PostgreSQL uses Linux information for authentication. In the previous example, you need a user called <strong>TheNewUser<\/strong>.<\/p><\/div>\n\n\n\n<\/p><p>Here are the PostgreSQL database setup steps:<\/p><ol class=\"wp-block-list\">\n<li>Run the following to switch to the <strong>postgres <\/strong>user and create a new database:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u postgres createdb TheNewUser<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>To log in to the new database, create a Linux user account with the same name. Since the username contains capital letters, add the <strong>&ndash;force-badname<\/strong> option.<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo adduser TheNewUser --force-badname<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Enter the interactive PostgreSQL prompt using the new user account:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo -u TheNewUser psql<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Confirm the connection using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">\\conninfo<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\"><img decoding=\"async\" width=\"1024\" height=\"102\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\/public\" alt=\"Terminal output confirms the new PostgreSQL user is connected to the new database\" class=\"wp-image-93045\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Terminal-output-confirms-the-new-PostgreSQL-user-is-connected-to-the-new-database.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>The user should now connect to the new database and be able to create a new table. For example, enter the following to create a buyer data table:<\/p><pre class=\"wp-block-preformatted\">CREATE TABLE buyers(usr_id INT PRIMARY KEY, usr_name VARCHAR(240) NOT NULL, usr_location VARCHAR(240) NOT NULL);<\/pre><p>When creating a database table, consider the six PostgreSQL constraints:<\/p><ul class=\"wp-block-list\">\n<li><strong>PRIMARY KEY <\/strong>&ndash; indicates that a column can be a row&rsquo;s unique identifier.<\/li>\n\n\n\n<li><strong>FOREIGN KEY <\/strong>&ndash; specifies that the values in a column must be the same as the ones in another table&rsquo;s rows.<\/li>\n\n\n\n<li><strong>NOT NULL <\/strong>&ndash; ensures a column&rsquo;s value is not empty.<\/li>\n\n\n\n<li><strong>CHECK<\/strong> &ndash; sets a column&rsquo;s value so that it must satisfy a Boolean expression, which is either &ldquo;true&rdquo; or &ldquo;false&rdquo;.<\/li>\n\n\n\n<li><strong>UNIQUE <\/strong>&ndash; guarantees that the values in a column are unique among all rows.<\/li>\n\n\n\n<li><strong>EXCLUSION <\/strong>&ndash; prevents values in two rows from overlapping when compared.<\/li>\n<\/ul><p>To add values to your table, use the following statement. Don&rsquo;t forget to replace the placeholders with the appropriate value and adjust the number of rows accordingly:<\/p><pre class=\"wp-block-preformatted\">INSERT INTO tablename (column1, column2, column3) VALUES (row1, row2, row3);<\/pre><p>To show the table, enter the following statement:<\/p><pre class=\"wp-block-preformatted\">SELECT * FROM tablename<\/pre><h3 class=\"wp-block-heading\" id=\"h-7-configure-the-postgresql-server\">7. Configure the PostgreSQL Server<\/h3><p>Configure your PostgreSQL server to allow other systems to access the database remotely. To do so, let the server listen to all IP addresses by editing the configuration file.<\/p><p>Before proceeding, use the <strong>exit <\/strong>command to quit the PostgreSQL shell and return as the regular system user. Then, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>Open the configuration file using a text editor. Replace <strong>vim <\/strong>if you use another editor and change the directory accordingly:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">vim \/etc\/postgresql\/14\/main\/postgresql.conf<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Find the <strong>listen_addresses<\/strong> line.<\/li>\n\n\n\n<li>Remove the <strong># <\/strong>symbol and change the value to an asterisk. Here&rsquo;s how it should look:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">listen_addresses = '*'<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Use the <strong>:wq<\/strong> command to save the changes and quit <strong>vim<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\"><img decoding=\"async\" width=\"1062\" height=\"58\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/public\" alt=\"The listen address line in the PostgreSQL configuration file\" class=\"wp-image-93046\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/w=1062,fit=scale-down 1062w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-listen-address-line-in-the-PostgreSQL-configuration-file.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1062px) 100vw, 1062px\" \/><\/a><\/figure><\/div><ol start=\"5\" class=\"wp-block-list\">\n<li>Set the access policy to authenticate client connection to your PostgreSQL server. To do so, open the <strong>pg_hba.conf<\/strong> file in the same directory using <strong>vim<\/strong>. The command should look like this:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">vim \/etc\/postgresql\/14\/main\/pg_hba.conf<\/pre><ol start=\"6\" class=\"wp-block-list\">\n<li>Add a new policy at the bottom of the file using this syntax:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">connection database user ip_address encryption<\/pre><ol start=\"7\" class=\"wp-block-list\">\n<li>For instance, we will enable all TCP\/IP connections from all users to every PostgreSQL database. These users can use any IP address with an MD5-encrypted password. The rule looks as follows:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">host all all 0.0.0.0\/0 md5<\/pre><ol start=\"8\" class=\"wp-block-list\">\n<li>Restart the PostgreSQL service to apply the changes.<\/li>\n<\/ol><h3 class=\"wp-block-heading\" id=\"h-8-set-up-a-remote-database-connection\">8. Set up a Remote Database Connection<\/h3><p>After configuring the database, set up remote access to PostgreSQL using the <strong>psql<\/strong> command. To do so, use this command syntax in the client machine:<\/p><pre class=\"wp-block-preformatted\">psql -h ip_address -p port -d database -U username<\/pre><p>For example, run this command to connect to the TheNewUser database hosted in a VPS with the <strong>185.185.185.185 <\/strong>IP address using the <strong>TheNewUser<\/strong> account:<\/p><pre class=\"wp-block-preformatted\">psql -h 185.185.185.185 -p 5432 -d TheNewUser -U TheNewUser<\/pre><p>Remember that the default PostgreSQL port is <strong>5423<\/strong>. To verify if the connection is successful, run this command:<\/p><pre class=\"wp-block-preformatted\">\\conninfo<\/pre><p>Once connected, you can send database queries to retrieve data. To enable PostgreSQL remote access in your application, install a database driver and import the module into your code.<\/p><p>For this tutorial, we will show you how to do so in a Python application hosted in an <strong>Ubuntu 22.04<\/strong> VPS. After connecting via SSH, follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>Install the <strong>pip package manager <\/strong>using this command. If you already have it, skip this step:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get install python-pip<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Download and install the <strong>psycopg2<\/strong> database driver using pip:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">pip install psycopg2-binary<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Add the following line to your application code to import the module and establish a connection with the PostgreSQL database. Remember to change the value accordingly:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">import psycopg2\n# Connect your app to the remote database.\nconn = psycopg2.connect(host=\"182.182.182.182\", port=\"5432\", dbname=\"TestNewUser\", user=\"TestNewUser\", password=\"UserPassword\")\n# Open a cursor for database operations\ncur = conn.cursor()\n# Execute a query. Replace table with your own. \ncur.execute(\"SELECT * FROM table\")\n# Retrieve data\nrecords = cur.fetchall()<\/pre><h3 class=\"wp-block-heading\" id=\"h-9-install-pgadmin\">9. Install pgAdmin<\/h3><p>Install the<strong> pgAdmin<\/strong> web-based GUI to simplify your PostgreSQL database management tasks. We will use the APT package manager to install it:<\/p><ol class=\"wp-block-list\">\n<li>Run these commands individually to add the pgAdmin official repository:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">curl -fsSL https:\/\/www.pgadmin.org\/static\/packages_pgadmin_org.pub | sudo gpg --dearmor -o \/etc\/apt\/trusted.gpg.d\/pgadmin.gpg\n\nsudo sh -c 'echo \"deb https:\/\/ftp.postgresql.org\/pub\/pgadmin\/pgadmin4\/apt\/$(lsb_release -cs) pgadmin4 main\" &gt; \/etc\/apt\/sources.list.d\/pgadmin4.list'<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Update the package lists to synchronize the repository.<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt update<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Run the following command to install pgAdmin:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt install pgadmin4<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>After the GUI setup finishes, initiate the primary pgAdmin configuration with this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo \/usr\/pgadmin4\/bin\/setup-web.sh<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Enter your email address and password. It should look like the following:<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\"><img decoding=\"async\" width=\"1238\" height=\"402\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/public\" alt=\"The bash script to initiate pgAdmin configuration\" class=\"wp-image-93047\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/w=1238,fit=scale-down 1238w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-bash-script-to-initiate-pgAdmin-configuration.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1238px) 100vw, 1238px\" \/><\/a><\/figure><\/div><p>To access the pgAdmin web-based interface, enter the following in your web browser&rsquo;s address bar:<\/p><pre class=\"wp-block-preformatted\">185.185.185.185\/pgadmin4<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\"><img decoding=\"async\" width=\"851\" height=\"543\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\/public\" alt=\"Login screen for pgAdmin GUI with the login button highlighted\" class=\"wp-image-93048\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\/w=851,fit=scale-down 851w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/Login-screen-for-pgAdmin-GUI-with-the-login-button-highlighted.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 851px) 100vw, 851px\" \/><\/a><\/figure><\/div><p>Replace the IP address with your VPS&rsquo;. Then, enter your email address and password. Click <strong>Login<\/strong> to enter the<strong> <\/strong>main pgAdmin dashboard.<\/p><p>To connect PostgreSQL, select <strong>Add New Server<\/strong> and proceed with the configuration process.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"824\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\/public\" alt=\"The main management window for pgAdmin with the Add New Server button highlighted\" class=\"wp-image-93049\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/03\/The-main-management-window-for-pgAdmin-with-the-Add-New-Server-button-highlighted.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-troubleshooting-postgresql-installation\">Troubleshooting PostgreSQL Installation<\/h2><p>In this section, we will explore common issues regarding PostgreSQL installation and their solutions.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-unable-to-connect-to-postgresql-server\">1. Unable to Connect to PostgreSQL Server<\/h3><p>First, you should check whether the client system is properly connected to the internet. If the internet connection isn&rsquo;t the issue, other common causes for this error include an inactive database or misconfigured file permission.<\/p><p>Check PostgreSQL status and ensure it is <strong>loaded <\/strong>and <strong>active<\/strong>. Otherwise, restart it using the following command:<\/p><pre class=\"wp-block-preformatted\">sudo systemctl restart postgresql<\/pre><p>If that doesn&rsquo;t work, check whether the PostgreSQL directory and files permissions are set to <strong>0700<\/strong> and <strong>0600<\/strong>. To do so, use the following command:<\/p><pre class=\"wp-block-preformatted\">ls \/var\/lib\/postgresql\/main<\/pre><p>Then, use the <a href=\"\/tutorials\/how-to-change-linux-permissions-and-owners\"><strong>chmod <\/strong>command to change their permissions<\/a>.<\/p><h3 class=\"wp-block-heading\" id=\"h-2-postgresql-connection-issues-from-remote-host\">2. PostgreSQL Connection Issues from Remote Host<\/h3><p>A misconfigured connection policy may cause remote host issues that prevent you from accessing the PostgreSQL server. To solve it, open your <strong>pg_hba.conf<\/strong> file and add the access policy from every client&rsquo;s IP address:<\/p><pre class=\"wp-block-preformatted\">host all all 0.0.0.0\/0 md5<\/pre><p>Then, check the <strong>listen_address <\/strong>value in your <strong>postgres.conf <\/strong>configuration file. Ensure it has an <strong>asterisk<\/strong> to ensure PostgreSQL can listen to all IP addresses.<\/p><p>If that doesn&rsquo;t work, check if your firewall closes the PostgreSQL listening port. For Ubuntu server, run the <strong>ufw <\/strong>command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw status verbose<\/pre><p>If the connection from and to port <strong>5432<\/strong> is disabled, open it using the following command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow 5432\/tcp <\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Learn more about Ubuntu Firewall<\/h4>\n                    <p> Check out our other article to learn <a href=\"\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw\/\">how to configure Ubuntu firewall using Uncomplicated Firewall (UFW)<\/a>.<\/p>\n                <\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-3-pgadmin-login-issues\">3. pgAdmin Login Issues<\/h3><p>pgAdmin login issues may occur due to a database connection error. If it is network-related, try the previous solutions and reinstall pgAdmin.<\/p><p>If the problem is related to the user account login credentials, enter the <strong>psql <\/strong>session<strong> <\/strong>and reset the password using this statement:<\/p><pre class=\"wp-block-preformatted\">ALTER USER username WITH PASSWORD 'new_password';<\/pre><p>You may also encounter login issues if the user doesn&rsquo;t have the<strong> LOGIN<\/strong> privilege. To grant one, use the following statement:<\/p><pre class=\"wp-block-preformatted\">ALTER USER username WITH LOGIN;<\/pre><h3 class=\"wp-block-heading\" id=\"h-4-postgresql-version-conflict\">4. PostgreSQL Version Conflict<\/h3><p>Version conflicts occur when PostgreSQL is incompatible with the database or other applications. Its common cause includes not updating PostgreSQL, resulting in a version mismatch.<\/p><p>To solve it, reinstall or update PostgreSQL from the APT official repository. Also, update all third-party applications since they may be outdated.<\/p><p>We recommend testing the newest version in a development environment to avoid further issues in the live server.<\/p><h3 class=\"wp-block-heading\" id=\"h-5-unable-to-create-postgresql-user-or-database\">5. Unable to Create PostgreSQL User or Database<\/h3><p>An incorrect command syntax often triggers PostgreSQL user and database creation errors. To fix it, ensure your command ends with a semicolon (<strong>;<\/strong>).<\/p><p>Also, you must run the command using the <strong>postgres<\/strong> user and the <strong>psql <\/strong>command line. To do so, run this command:<\/p><pre class=\"wp-block-preformatted\">sudo -u postgres psql<\/pre><p>If your Terminal shows the following, you should be able to create a user and database.<\/p><pre class=\"wp-block-preformatted\">postgres=#<\/pre><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Learn More About PostgreSQL and VPS<\/h4>\n                    <p><a href=\"\/tutorials\/getting-started-with-vps-hosting\">How to Set Up a VPS<\/a><br>\n<a href=\"\/tutorials\/what-is-vps-hosting\">What is a VPS<\/a><br>\n<a href=\"\/tutorials\/how-to-install-postgresql-on-centos-7\/\">how to install PostgreSQL on CentOS7<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>PostgreSQL is an open-source, community-driven database management system. It uses a logical structure to efficiently store large amounts of data and simplify access.<\/p><p>In this relational database management system tutorial, we have explained the steps to install PostgreSQL on Ubuntu 22.04 via the Linux command prompt. Here is the recap:<\/p><ol class=\"wp-block-list\">\n<li>Install PostgreSQL from the official repository using <strong>APT<\/strong>.<\/li>\n\n\n\n<li>Run <strong>systemctl status <\/strong>to check if Postgresql is active.<\/li>\n\n\n\n<li>Connect to the PostgreSQL account using <strong>sudo su &ndash; postgres<\/strong>.<\/li>\n\n\n\n<li>Change the default admin account&rsquo;s password to improve security.<\/li>\n\n\n\n<li>Create a new PostgreSQL user to manage your databases.<\/li>\n\n\n\n<li>Make a new database using the <strong>createdb <\/strong>command.<\/li>\n\n\n\n<li>Set up the PostgreSQL server by configuring its connection policy.<\/li>\n\n\n\n<li>Configure a connection to your remote PostgreSQL database.<\/li>\n\n\n\n<li>Install pgAdmin to provide a graphical user interface.<\/li>\n<\/ol><p>To avoid issues, ensure the connection policies and pgAdmin login credentials are set correctly. Moreover, install the latest version of the RDBMS via APT to avoid incompatibility issues.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-postgresql-on-ubuntu-faq\">How to Install PostgreSQL on Ubuntu FAQ<\/h2><p>In this section, we will answer several commonly asked questions about installing PostgreSQL on an Ubuntu server.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1693491145690\"><h3 class=\"schema-faq-question\">Where Is Postgres Installed on Ubuntu?&nbsp;<\/h3> <p class=\"schema-faq-answer\">On Ubuntu, the main PostgreSQL server files are in the <strong>\/usr\/lib\/postgresql\/&lt;version\/bin\/postgres <\/strong>directory. All database clusters are stored in <strong>\/var\/lib\/postgresql\/main<\/strong> directory. Meanwhile, the PostgreSQL settings are in the <strong>\/etc\/postgresql\/main\/postgresql.conf <\/strong>file.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1693491155361\"><h3 class=\"schema-faq-question\">How Do I Know if PostgreSQL Is Installed on Ubuntu?<\/h3> <p class=\"schema-faq-answer\">The easiest way to check if PostgreSQL is installed on an Ubuntu server is to run <strong>sudo -u postgres psql<\/strong> or <strong>which psql<\/strong>. Terminal should output the installation path if it is installed.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1693491165378\"><h3 class=\"schema-faq-question\">Should I Use PostgreSQL 13 or 14?<\/h3> <p class=\"schema-faq-answer\">PostgreSQL 14 has an improved connection scaling. It makes PostgreSQL 14 more suitable for large enterprises with many database connections. We recommend sticking to the newest version for the latest features, improvements, and patches.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL is a popular open-source relational database management system (RDBMS) known for its scalability and efficiency in handling large amounts [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/how-to-install-postgresql-on-ubuntu\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Install PostgreSQL on Ubuntu + pgAdmin Setup","rank_math_description":"Learn how to install postgreSQL on Ubuntu 22.04 using APT and local repository. pgAdmin installation and usage guide included.","rank_math_focus_keyword":"install postgresql ubuntu","footnotes":""},"categories":[22646,22644],"tags":[],"class_list":["post-15945","post","type-post","status-publish","format-standard","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/instalar-postgresql-ubuntu","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/install-postgresql-ubuntu","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/que-son-hooks-wordpress-8","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/que-son-hooks-wordpress-8","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/instalar-postgresql-ubuntu","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-install-postgresql-on-ubuntu","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15945"}],"version-history":[{"count":29,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15945\/revisions"}],"predecessor-version":[{"id":133902,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15945\/revisions\/133902"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=15945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=15945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}