PostgreSQL is a powerful open-source relational database management system known for its robustness and flexibility. With each new release, PostgreSQL continues to improve and offer new features. Ubuntu 22.04, the latest LTS (Long Term Support) release of Ubuntu, provides an excellent platform for running PostgreSQL 16. In this blog post, we will guide you through the steps to install PostgreSQL 16 on Ubuntu 22.04.
Prerequisites
Before you start the installation process, make sure you have the following:
- An Ubuntu 22.04 server or virtual machine with sudo privileges.
- A terminal or SSH access to your Ubuntu machine.
- A basic understanding of working with the command line.
Now, let’s dive into the installation process!
Step 1: Update System Packages
First, it’s essential to ensure that your system is up to date. Open a terminal and run the following command to update the package list and upgrade existing packages:
$ sudo apt update && sudo apt upgrade -y
This command will fetch the latest package information and install any available updates.
Step 2: Add PostgreSQL APT Repository
PostgreSQL provides an official APT repository for Ubuntu, which makes it easy to install the latest version. To add the PostgreSQL APT repository to your system, run the following commands:
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
The above command adds the PostgreSQL APT repository to your system’s package sources.
Next, import the repository’s signing key using the following command:
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Step 3: Install PostgreSQL 16
With the repository added and the key imported, it’s time to install PostgreSQL 16. Run the following command:
$ sudo apt update
$ sudo apt install postgresql-16 postgresql-contrib-16
The postgresql-contrib-16
package includes additional utilities and extensions for PostgreSQL.
Step 4: Start and Enable PostgreSQL
After PostgreSQL 16 is installed, it is not automatically started. You need to enable and start the PostgreSQL service using the following commands:
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql
Step 5: Set a Password for the PostgreSQL User (Optional)
By default, PostgreSQL creates a user named postgres
with administrative privileges. You should set a password for this user. To do this, switch to the postgres
user and access the PostgreSQL prompt:
$ sudo -i -u postgres
$ psql
Now, set a password for the postgres
user by running the following SQL command:
ALTER USER postgres PASSWORD 'your_password';
Replace 'your_password'
with your desired password. After setting the password, exit the PostgreSQL prompt by typing \q
and pressing Enter.
Step 6: Configure PostgreSQL for Remote Access (Optional)
If you plan to access PostgreSQL from remote machines, you may need to modify the PostgreSQL configuration to allow remote connections. By default, PostgreSQL only allows connections from the localhost.
To enable remote access, open the PostgreSQL configuration file in a text editor:
$ sudo nano /etc/postgresql/16/main/postgresql.conf
Find the listen_addresses
line and change it to:
listen_addresses = '*'
Save the file and exit the text editor.
Next, edit the pg_hba.conf
file:
$ sudo nano /etc/postgresql/16/main/pg_hba.conf
Add the following line at the end of the file to allow all IPv4 and IPv6 addresses to connect:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Save the file and exit the text editor.
Finally, restart PostgreSQL to apply the changes:
$ sudo systemctl restart postgresql
Conclusion
Congratulations! You’ve successfully installed PostgreSQL 16 on your Ubuntu 22.04 machine. PostgreSQL is now up and running, ready for you to create databases, tables, and start building your applications with this powerful relational database management system. If you have any specific use cases or configurations in mind, don’t forget to explore the official PostgreSQL documentation for further guidance. Enjoy using PostgreSQL on your Ubuntu 22.04 server!
Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.