To install PostgreSQL 16 on Ubuntu, follow these steps:
Before installing PostgreSQL, ensure that your system is up-to-date.
sudo apt update
sudo apt upgrade -y
The PostgreSQL APT repository contains the latest stable PostgreSQL releases. Add it to your system.
- Install prerequisites for adding a new repository:
sudo apt install -y wget gnupg
- Add the PostgreSQL repository key:
wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg \
--dearmor -o /usr/share/keyrings/postgresql-archive-keyring.gpg
- Add the PostgreSQL repository:
echo "deb [signed-by=/usr/share/keyrings/postgresql-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt/ \
$(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
- Update the package lists:
sudo apt update
- Install PostgreSQL 16:
sudo apt install -y postgresql-16
- Check the PostgreSQL service status:
sudo systemctl status postgresql
- Verify the PostgreSQL version:
psql --version
- Switch to the postgres user:
sudo -i -u postgres
- Access the PostgreSQL shell:
psql
- Exit the shell:
\q
- Return to your normal user account:
exit
• If PostgreSQL does not start automatically, start it manually:
sudo systemctl start postgresql
• Enable PostgreSQL to start on boot:
sudo systemctl enable postgresql
PostgreSQL 16 is now installed and ready for use!