How to Install PostgreSQL 16 on Ubuntu 22.04

How to Install PostgreSQL 16 on Ubuntu 22.04

An easy way to install the latest PostgreSQL 16 in Ubuntu 22.04, for this, you need to install some required packages and then run sudo apt install postgresql-16 postgresql-contrib-16 on the terminal window to install it.

How to Install PostgreSQL 16 on Ubuntu 22.04

Steps to install and configure latest PostgreSQL 16 on Ubuntu 22.04:

Step 1 – Update the Package List

Press ctrl+alt+t on the keyboard to start a terminal window and type sudo apt update to update the package lists:

sudo apt update
sudo apt install gnupg2 wget vim

Step 2 – Add PostgreSQL Repository

To add postgreSQL repository from apt source list, simply type the following command on terminal window:

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

Next, import the repository signing key by typing the following command on terminal window:

curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg

Step 3 – Install PostgreSQL 16 on Ubuntu 22.04

Run sudo apt install postgresql-16 postgresql-contrib-16 command on terminal window and press enter to install postgreSQL on Ubuntu system:

sudo apt install postgresql-16 postgresql-contrib-16

By default PostgreSQL is disabled and stopped after installation, to enable and start it you can run the sudo systemctl start postgresql && sudo systemctl enable postgresql command on a terminal window:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Step 4 – Configure PostgreSQL 16

To allow remote connections you need to open and edit the postgresql.conf file and add listen_address to it, you can do this with sudo nano /etc/postgresql/16/main/postgresql.conf command:

sudo nano /etc/postgresql/16/main/postgresql.conf

And add the listen_address into it; as follows:

listen_addresses = '*'

As you want to remotely connect to PostgreSQL from PGADMIN, you need to edit the password authentication in pg_hba.conf, which you can do with the help of the following commands:

sudo sed -i '/^host/s/ident/md5/' /etc/postgresql/16/main/pg_hba.conf
sudo sed -i '/^local/s/peer/trust/' /etc/postgresql/16/main/pg_hba.conf
echo "host all all 0.0.0.0/0 md5" | sudo tee -a /etc/postgresql/16/main/pg_hba.conf

To run the sudo systemctl restart postgresql command to restart PostgreSQL for changes to take effect:

sudo systemctl restart postgresql

Allow Firewall for PostgreSQL port by using the sudo ufw allow 5432/tcp command:

sudo ufw allow 5432/tcp

Step 5 – Connect to PostgreSQL

To connect as a PostgreSQL user, run the sudo -u postgres psql command on a terminal window:

sudo -u postgres psql

Now you can set the password for the PostgreSQL user, for this you need to run the ALTER USER postgres PASSWORD 'VeryStronGPassWord@1157'; command on the terminal window:

ALTER USER postgres PASSWORD 'VeryStronGPassWord@1157';

Conclusion

That’s it; you have learned how to install and use postgreSQL 16 on ubuntu 22.04.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *