
PostgreSQL is a open-source relational database management system known for its advanced features, reliability, and strong support for SQL and JSON data types. It is trusted across industries, from web applications and real-time analytics to Geographic Information Systems (GIS) and healthcare platforms, because of its flexibility, extensibility, and data integrity. When paired with Ubuntu 22.04, a long-term support (LTS) Linux distribution recognized for its stability, security, and performance, it becomes an ideal foundation for deploying scalable and production-grade database systems. Ubuntu’s lightweight and developer-friendly environment, combined with PostgreSQL’s robust capabilities, provides a reliable and secure platform for managing data in modern applications across cloud, virtual, and physical servers.
This article explains how to install PostgreSQL on Ubuntu 22.04 and deploy a PostgreSQL database server while securing it for access by trusted users on the server. If you're working with a different system, you can also check out our article on how to install PostgreSQL on FreeBSD 12.2.
Before you begin:
This section shows you how to install the PostgreSQL database server package, ideal for anyone installing PostgreSQL on Ubuntu 22.04, whether for development or production use.
Add the PostgreSQL repository to your server's APT sources.
Import the PostgreSQL repository key to your server using the wget utility.
Update the server packages to synchronize the new PostgreSQL repository.
Install PostgreSQL on your server.
Start the PostgreSQL database server.
View the PostgreSQL service status and verify that it's active.
Output:
The psql client tool creates a connection to the PostgreSQL database server with support for operations such as SQL statement execution and remote database access. Follow the steps below to use the PostgreSQL client tool to connect and access your database server.
Log in to the PostgreSQL Database server using the default postgres user.
Create a new sample database hospital.
Switch to the new database.
Create a new sample doctors table
In the above SQL statement:
doctor_id column is a PRIMARY KEY that uniquely identifies each doctor in the doctors table.first_name and last_name columns store names in the doctors table.appointment_date stores the doctor's appointment date with a patient in the hospital.SERIAL generates a new doctor_id for each new record.Insert sample data into the doctors table
Query the doctors table to view all available data.
Output:
Exit the PostgreSQL console.
Follow the steps below to secure the PostgreSQL database server with password authentication to enable only authorized users to access databases.
Log in to the PostgreSQL database server.
Modify the default postgres user with a new strong password.
Exit the PostgreSQL console
Back up the default pg_hba.conf configuration file for recovery purposes. Replace 16 with your actual database server version.
Open the pg_hba.conf configuration file using a text editor such as Nano
Find the following section within the file.
Change the peer value to password to enable password authentication on the PostgreSQL database server.
Restart the PostgreSQL server to apply the new configuration changes.
Log in to the PostgreSQL Database server using the postgres user to test the new authentication.
Enter the postgres user password you set earlier and press Enter to access the database server.
Create a new user role db_manager with a secure password.
Grant the new user full privileges to the hospital database.
Exit the PostgreSQL database console.
Log in to the PostgreSQL database as the new user db_manager user to test access to the hospital database.
Enter the user password when prompted and press Enter to access the database.
You have installed the PostgreSQL server on Ubuntu 22.04 and used the psql utility to access the database server. In addition, you created a new database table with sample data, a new role with database privileges, and enabled secure user password authentication. For more information and database configurations, visit the official PostgreSQL documentation.
0 Comments
Be the first to comment and share your perspective with the community.