---
description: PostgreSQL is a well-known open-source Relational Database Management System (RDBMS).
title: How to Install PostgreSQL on Ubuntu (pgAdmin Setup)
image: https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Installing PostgreSQL on Ubuntu uses two reliable methods: the Ubuntu apt repository or the official PostgreSQL APT source. Lessons cover prerequisites, user and database management, security hardening, pgAdmin setup, and clean uninstall steps.

* 📦 **Two Paths:** Ubuntu apt for stable; PostgreSQL APT for newest.
* 🔧 **Prerequisites:** sudo privileges and an Ubuntu LTS host.
* 👤 **User Mgmt:** createuser, ALTER USER, DROP USER manage roles.
* 🗄️ **Database Mgmt:** createdb, dropdb, \\l in psql cover routine ops.
* 🛡️ **Secure Defaults:** Change postgres password and tighten pg\_hba.conf.
* 🖥️ **pgAdmin:** Browser-based admin console for visual users.

[ Read More ](javascript:void%280%29;) 

![How to Install PostgreSQL on Ubuntu](https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png)

## How to Install PostgreSQL on Ubuntu?

PostgreSQL is a well-known open-source **R**elational **D**atabase **M**anagement System (RDBMS). It is popular in terms of rich features, scalability, and stability. This RDBMS supports data integrity, complex queries, and concurrent access.

Most organizations select [PostgreSQL](https://www.guru99.com/introduction-postgresql.html) because of its extensibility, reliability, and its cross-platform nature. Moreover, it also provides a solid base for handling extensive amounts of data. This [RDMS](https://www.guru99.com/relational-data-model-dbms.html) is preferred by data analysts, developers, and businesses seeking a flexible and powerful database solution.

### Prerequisites

To follow our guide, you must have:

* **Ubuntu 22.04** installed on your system
* A **non-root user** with sudo privileges

Now, let’s check the procedure to install Postgres on Ubuntu using the following:

* The Ubuntu repository
* The PostgreSQL official repository

## Method 1: How to Install and Setup PostgreSQL on Ubuntu Using the Ubuntu Repository

In this method, we will utilize the Ubuntu repository for downloading and installing PostgreSQL. This repository already comprises the official packages updated and maintained by the Ubuntu developers. Ultimately, this makes sure that the installation process remains hassle-free and straightforward.

**Step 1)** To install PostgreSQL in Linux, firstly, update your system packages:

sudo apt update

**Output:**

[](https://www.guru99.com/images/4/install-postgresql-1.png)

[](https://www.guru99.com/images/4/install-postgresql-2.png)

**Step 2)** Then, go for PostgreSQL installation:

sudo apt install postgresql -y

[](https://www.guru99.com/images/4/install-postgresql-3.png)

**Step 3)** Lastly, check out the PostgreSQL version for verification:

psql --version

As you can see, PostgreSQL version 14.8 has been successfully installed on our Ubuntu system:

[](https://www.guru99.com/images/4/install-postgresql-4.png)

## Method 2: Install PostgreSQL on Ubuntu Using the PostgreSQL Official Repository

The official PostgreSQL repository permits you to access the latest version of PostgreSQL and the relevant features that are not available in the Ubuntu official repository. With PostgreSQL’s official repository, you can easily install and update PostgreSQL.

Now, follow the provided instructions to install PostgreSQL on Ubuntu.

**Step 1)** The first step is to add the repository of PostgreSQL:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

[](https://www.guru99.com/images/4/install-postgresql-5.png)

**Step 2)** Then, move toward adding PostgreSQL repository key:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

[](https://www.guru99.com/images/4/install-postgresql-6.png)

**Step 3)** Update your system packages to move ahead:

sudo apt update

[](https://www.guru99.com/images/4/install-postgresql-7.png)

**Step 4)** Now, go for PostgreSQL installation:

sudo apt install postgresql -y

[](https://www.guru99.com/images/4/install-postgresql-8.png)

**Step 5)** View the PostgreSQL version for confirmation:

psql --version

It can be observed that with the help of the PostgreSQL repository, we have installed its 15.3 version on Ubuntu:

[](https://www.guru99.com/images/4/install-postgresql-9.png)

### RELATED ARTICLES

* [PostgreSQL DROP/Delete DATABASE Using Command Line ](https://www.guru99.com/postgresql-drop-database.html "PostgreSQL DROP/Delete DATABASE Using Command Line")
* [How to Create User in PostgreSQL Using PgAdmin ](https://www.guru99.com/postgresql-create-alter-add-user.html "How to Create User in PostgreSQL Using PgAdmin")
* [PostgreSQL BETWEEN Query with Example ](https://www.guru99.com/postgresql-between.html "PostgreSQL BETWEEN Query with Example")
* [PostgreSQL Join Types: Inner, Outer, Left, Right ](https://www.guru99.com/postgresql-joins-left-right.html "PostgreSQL Join Types: Inner, Outer, Left, Right")

## How to Manage PostgreSQL Users on Ubuntu?

Management of PostgreSQL users involves the process of creating, modifying their access, and removing users to control the database privileges. It ensures that the administrator grants required permissions and manages the [PostgreSQL user accounts](https://www.guru99.com/postgresql-create-alter-add-user.html) in an effective manner.

**Step 1)** To create a new Postgres user on Ubuntu, run this command:

sudo -u postgres createuser --interactive

As a result, PostgreSQL prompt to type out the username and confirm whether the newly created user should have the superuser privileges:

[](https://www.guru99.com/images/4/install-postgresql-10.png)

In our case, we have entered “huzail” as a username and permitted it to be a super user.

**Step 2)** You can also separately give sudo privileges to the user by specifying its name in this command:

ALTER USER huzail WITH SUPERUSER;sudo -u postgres psql

[](https://www.guru99.com/images/4/install-postgresql-11.png)

**Step 3)** In case you want to drop the newly created user, write out its name in the “DROP USER” command:

DROP USER huzail;

[](https://www.guru99.com/images/4/install-postgresql-12.png)

## How to Manage PostgreSQL Database on Ubuntu?

Next comes the management of the PostgreSQL database on Ubuntu. This involves different operations related to creating, modifying, and modifying databases for better organization.

**Step 1)** First of all, utilize the “[CREATE DATABASE](https://www.guru99.com/postgresql-create-database.html)” command for creating a database and specify its name as we added “guru99” here:

CREATE DATABASE guru99;

[](https://www.guru99.com/images/4/install-postgresql-13.png)

**Step 2)** Then, grant the database access to the required user:

GRANT ALL PRIVILEGES ON DATABASE guru99 TO huzail;

[](https://www.guru99.com/images/4/install-postgresql-14.png)

In our case, we have granted the “guru99” access to the user “huzail”:

[](https://www.guru99.com/images/4/install-postgresql-15.png)

**Step 3)** In case you want to delete or drop the database, run “DROP DATABASE” command with the desired database name:

DROP DATABASE guru99;

Resultantly, the given database will get deleted:

[](https://www.guru99.com/images/4/install-postgresql-13.png)

A fresh install needs hardening before being exposed to applications.

## Securing Default PostgreSQL User

The operation of securing the default PostgreSQL user comprises different measures, such as configuring a strong password, enabling the relevant password encryption, and restarting the PostgreSQL service. By doing so, the default user will be protected from unauthorized access.

**Step 1)** Firstly, log in to the PostgreSQL database as we already did:

sudo -u postgres psql

**Step 2)** Then, add the password for the default PostgreSQL user:

ALTER USER postgres PASSWORD 'my-secret-password123';

[](https://www.guru99.com/images/4/install-postgresql-16.png)

**Step 3)** Open up the relevant configuration file for enabling the password encryption:

sudo nano /etc/postgresql/{version}/main/pg_hba.conf

In the above command, replace the version number with the PostgreSQL version you have installed, as we added “15” here:

[](https://www.guru99.com/images/4/install-postgresql-17.png)

Your PostgreSQL configuration file will somehow look like this:

[](https://www.guru99.com/images/4/install-postgresql-18.png)

Look for the line that starts with “local all all” and change the encryption method from “peer” to “md5”. After that, hit “CTRL+O” to save changes and press CTRL+X” to exit the nano editor:

[](https://www.guru99.com/images/4/install-postgresql-19.png)

**Step 4)** Restart the PostgreSQL service so that it can take effect from the configuration changes:

sudo service postgresql restart

[](https://www.guru99.com/images/4/install-postgresql-20.png)

**Step 5)** Lastly, check PostgreSQL status:

sudo service postgresql status

[](https://www.guru99.com/images/4/install-postgresql-21.png)

That’s it! You have secured the default PostgreSQL user with the encrypted password.

### Bonus Step: Install pgAdmin

[pgAdmin](https://www.pgadmin.org/) is a Graphical User Interface (GUI) PostgreSQL client that simplifies the management tasks. It also enables efficient query execution, database administration, and PostgreSQL server monitoring. Moreover, it also assists in enhancing the productivity of PostgreSQL users.

**Step 1)** First of all, you have to add the PostgreSQL repository key:

url -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/pgadmin.gpg

In our case, we have already added it, so we will press “y” for overwriting it:

[](https://www.guru99.com/images/4/install-postgresql-22.png)

sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'

**Step 2)** Then, add the pgAdmin repository to your Ubuntu system:

[](https://www.guru99.com/images/4/install-postgresql-23.png)

**Step 3)** Next, update packages for further proceeding:

Sudo apt update

[](https://www.guru99.com/images/4/install-postgresql-24.png)

**Step 4)** Now, install pgAdmin4 by typing out this command:

sudo apt install pgadmin4

[](https://www.guru99.com/images/4/install-postgresql-25.png)

[](https://www.guru99.com/images/4/install-postgresql-26.png)

**Step 5)** Now, you have to enter the given command for initiating the primary configuration of pgAdmin:

sudo /usr/pgadmin4/bin/setup-web.sh

Then, specify the required information:

[](https://www.guru99.com/images/4/install-postgresql-27.png)

**Step 6)** Launch pgAdmin4 on your web browser by navigating to the link that was provided in the last output:

[](https://www.guru99.com/images/4/install-postgresql-28.png)

Use your pre-configured email and password to login to pgAdmin:

[](https://www.guru99.com/images/4/install-postgresql-29.png)

Here comes the main dashboard of the pgAdmin that is all ready to use:

[](https://www.guru99.com/images/4/install-postgresql-30.png)

## How to Uninstall PostgreSQL from Ubuntu?

**Step 1)** In other to uninstall PostgreSQL from your Ubuntu system, firstly, stop the PostgreSQL service:

sudo systemctl stop postgresql

[](https://www.guru99.com/images/4/install-postgresql-31.png)

**Step 2)** Then, remove PostgreSQL and its relevant dependencies in case you have installed them earlier:

sudo apt purge postgresql postgresql-contrib -y

[](https://www.guru99.com/images/4/install-postgresql-32.png)

**Step 3)** Remove all configuration files of the PostgreSQL:

sudo rm -rf /etc/postgresql/

[](https://www.guru99.com/images/4/install-postgresql-33.png)

Resultantly, [PostgreSQL](https://www.guru99.com/postgresql-tutorial.html) will be removed from your Ubuntu system.

### How to Uninstall pgAdmin from Ubuntu?

**Step 1)** In case it is required to remove pgAdmin from Ubuntu, remove its package first:

sudo apt remove pgadmin4

[](https://www.guru99.com/images/4/install-postgresql-34.png)

**Step 2)** Lastly, remove the relevant configuration files from your system:

sudo rm -rf ~/.pgadmin

[](https://www.guru99.com/images/4/install-postgresql-35.png)

That’s it! You have successfully uninstalled pgAdmin from Ubuntu.

## FAQs

⚡ Best PostgreSQL version on Ubuntu 24.04?

Ubuntu 24.04 ships PostgreSQL 16\. Use the official PostgreSQL APT repo for versions 17 or 18 with faster updates.

🤖 How does AI help new PostgreSQL admins?

AI assistants explain pg\_hba.conf entries, suggest tuning values for shared\_buffers and work\_mem, and generate apt or psql one-liners.

💡 Can AI write secure PostgreSQL install scripts?

Yes. AI tools generate Ansible, Terraform, and Bash scripts that install PostgreSQL, harden defaults, configure backups, and enable TLS.

📦 Should I use apt or the official PostgreSQL repo?

Use apt for stable, vendor-tested versions. Use the official PostgreSQL APT repo when you need newer releases or faster point updates.

🔑 How do I change the default postgres password?

Run sudo -i -u postgres, then psql and \\password to set a new password. Update pg\_hba.conf to require it from external connections.

🖥️ Is pgAdmin the only GUI for PostgreSQL on Ubuntu?

No. Beekeeper Studio, DBeaver, and DataGrip are popular cross-platform alternatives. pgAdmin remains the official community-maintained tool.

🛡️ How to back up a PostgreSQL database on Ubuntu?

Use pg\_dump for single-database logical backups, or pg\_dumpall for full clusters. pgBackRest and barman support point-in-time recovery.

📚 How to uninstall PostgreSQL completely from Ubuntu?

Run sudo apt-get –purge remove postgresql\\\*. Delete remaining data with sudo rm -rf /var/lib/postgresql and /etc/postgresql, then reboot.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png","url":"https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png","width":"700","height":"250","caption":"How to Install PostgreSQL on Ubuntu","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/postgresql","name":"PostgreSQL"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html","name":"How to Install PostgreSQL on Ubuntu (pgAdmin Setup)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#webpage","url":"https://www.guru99.com/install-postgresql-on-ubuntu.html","name":"How to Install PostgreSQL on Ubuntu (pgAdmin Setup)","dateModified":"2026-06-23T17:23:32+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/juniper","name":"Juniper Willow","description":"I am Juniper Willow, a PostgreSQL Developer, offering expert guidance to help you optimize and master PostgreSQL database development.","url":"https://www.guru99.com/author/juniper","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/juniper-willow-author.png","url":"https://www.guru99.com/images/juniper-willow-author.png","caption":"Juniper Willow","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"PostgreSQL","headline":"How to Install PostgreSQL on Ubuntu (pgAdmin Setup)","description":"PostgreSQL is a well-known open-source Relational Database Management System (RDBMS).","keywords":"postgresql","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/juniper","name":"Juniper Willow"},"dateModified":"2026-06-23T17:23:32+05:30","image":{"@id":"https://www.guru99.com/images/how-to-install-postgresql-on-ubuntu.png"},"copyrightYear":"2026","name":"How to Install PostgreSQL on Ubuntu (pgAdmin Setup)","subjectOf":[{"@type":"HowTo","name":"How to Install PostgreSQL on Ubuntu?","description":"Most organizations select PostgreSQLbecause of its extensibility, reliability, and its cross-platform nature. Moreover, it also provides a solid base for handling extensive amounts of data. This RDMS is preferred by data analysts, developers, and businesses seeking a flexible and powerful database solution.","step":[{"@type":"HowToStep","name":"Method 1:","text":"How to Install and Setup PostgreSQL on Ubuntu Using the Ubuntu Repository","image":"http://www.guru99.com/images/4/install-postgresql-1.png","url":"https://www.guru99.com/install-postgresql-on-ubuntu.html#step1"},{"@type":"HowToStep","name":"Method 2:","text":"Install PostgreSQL on Ubuntu Using the PostgreSQL Official Repository","image":"http://www.guru99.com/images/4/install-postgresql-5.png","url":"https://www.guru99.com/install-postgresql-on-ubuntu.html#step2"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Best PostgreSQL version on Ubuntu 24.04?","acceptedAnswer":{"@type":"Answer","text":"Ubuntu 24.04 ships PostgreSQL 16. Use the official PostgreSQL APT repo for versions 17 or 18 with faster updates."}},{"@type":"Question","name":"How does AI help new PostgreSQL admins?","acceptedAnswer":{"@type":"Answer","text":"AI assistants explain pg_hba.conf entries, suggest tuning values for shared_buffers and work_mem, and generate apt or psql one-liners."}},{"@type":"Question","name":"Can AI write secure PostgreSQL install scripts?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools generate Ansible, Terraform, and Bash scripts that install PostgreSQL, harden defaults, configure backups, and enable TLS."}},{"@type":"Question","name":"Should I use apt or the official PostgreSQL repo?","acceptedAnswer":{"@type":"Answer","text":"Use apt for stable, vendor-tested versions. Use the official PostgreSQL APT repo when you need newer releases or faster point updates."}},{"@type":"Question","name":"How do I change the default postgres password?","acceptedAnswer":{"@type":"Answer","text":"Run sudo -i -u postgres, then psql and password to set a new password. Update pg_hba.conf to require it from external connections."}},{"@type":"Question","name":"Is pgAdmin the only GUI for PostgreSQL on Ubuntu?","acceptedAnswer":{"@type":"Answer","text":"No. Beekeeper Studio, DBeaver, and DataGrip are popular cross-platform alternatives. pgAdmin remains the official community-maintained tool."}},{"@type":"Question","name":"How to back up a PostgreSQL database on Ubuntu?","acceptedAnswer":{"@type":"Answer","text":"Use pg_dump for single-database logical backups, or pg_dumpall for full clusters. pgBackRest and barman support point-in-time recovery."}},{"@type":"Question","name":"How to uninstall PostgreSQL completely from Ubuntu?","acceptedAnswer":{"@type":"Answer","text":"Run sudo apt-get --purge remove postgresql*. Delete remaining data with sudo rm -rf /var/lib/postgresql and /etc/postgresql, then reboot."}}]}],"@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#schema-439621","isPartOf":{"@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/install-postgresql-on-ubuntu.html#webpage"}}]}
```
