PostgreSQL is a powerful open-source relational database management system known for its reliability, robustness, and extensive feature set. Installing PostgreSQL 15 on Ubuntu 20.04 is a straightforward process, and in this guide, we’ll walk you through the steps to get it up and running.
Step 1: Update Package Lists
Before installing any new software, it’s essential to ensure that your system’s package lists are up to date. Open a terminal and run the following command:
sudo apt update
This command fetches the latest package information from the Ubuntu repositories.
Step 2: Install PostgreSQL
Once the package lists are updated, you can proceed to install PostgreSQL 15 using the following command:
sudo apt install postgresql-15
During the installation process, you may be prompted to confirm the installation by typing ‘Y’ and then pressing ‘Enter’.
Step 3: Verify PostgreSQL Installation
After the installation is complete, PostgreSQL should be up and running automatically. You can verify the installation by checking the PostgreSQL service status using the following command:
sudo systemctl status postgresql
If PostgreSQL is running properly, you should see a message indicating that it is active and running.
Step 4: Access PostgreSQL
By default, PostgreSQL creates a system user named ‘postgres’ with administrative privileges. You can switch to this user to access the PostgreSQL shell by running the following command:
sudo -i -u postgres
Once you’re logged in as the ‘postgres’ user, you can access the PostgreSQL shell by running the following command:
psql
This command will open the PostgreSQL shell, allowing you to interact with the database.
Step 5: Secure PostgreSQL
It’s crucial to secure your PostgreSQL installation by setting up authentication and access controls. By default, PostgreSQL uses a ‘peer’ authentication method, which means it trusts the system user’s identity. You may want to change this to a more secure authentication method, such as password authentication or certificate-based authentication. Additionally, you should configure firewall rules to restrict access to PostgreSQL ports.
Step 6: Create a Database
Once you’re logged into the PostgreSQL shell, you can create a new database using the following command:
CREATE DATABASE mydatabase;
Replace ‘mydatabase’ with the desired name of your database.
Step 7: Exit PostgreSQL Shell
After you’ve finished working with the PostgreSQL shell, you can exit by typing:
\q
This command will exit the PostgreSQL shell and return you to the system prompt.
Congratulations! You have successfully installed PostgreSQL 15 on your Ubuntu 20.04 system. You can now start using PostgreSQL to create, manage, and query databases for your applications.
Remember to configure any necessary firewall rules to allow traffic to PostgreSQL if you haven’t done so already. Additionally, you may want to explore further configuration options and best practices in the PostgreSQL documentation.