
PostgreSQL is an open-source, advanced RDBMS designed for managing a wide range of data tasks. It supports SQL to handle both small and large applications, including analytics, GIS, healthcare, and dynamic web apps.
In this article, you are to install PostgreSQL on an Ubuntu 22.04 server, and finally secure it for production use on your server.
Before you begin:
PostgreSQL is available in Ubuntu's default APT repositories. Follow the steps below to install PostgreSQL and configure it to start automatically at boot.
Update the server package index.
Install the postgresql-common dependency package on your server.
Run the following command to execute the PostgreSQL APT repository script.
Press Enter when prompted to add the new repository to your server sources.
Install the postgresql database server package.
Start the PostgreSQL database server.
View the PostgreSQL system service status and verify that it's active.
Output:
PostgreSQL uses the default postgres user account. Follow the steps below to enable password authentication and secure the server by granting access only to authorized users.
Check your installed PostgreSQL version.
Output:
Log in to the PostgreSQL database server using the postgres user account.
Modify the default postgres user with a new strong password.
Create a new user db_manager with a new strong password.
Exit the PostgreSQL console.
Run the following command to change the default peer value in the scram-sha-256 field in the main PostgreSQL configuration file pg_hba.conf to enable password authentication on the server.
Replace 17 with your installed PostgreSQL version number if it's different.
Restart the PostgreSQL server to apply the new configuration changes.
Access the PostgreSQL database console using the pre-installed psql utility or graphical tools for a direct connection. Follow the steps below to create a new sample database for use with a non-privileged user.
Create a new sample PostgreSQL database hospital and grant the db_manager user ownership privileges to the database.
When prompted, enter the Postgres user password you created earlier.
Log in to the PostgreSQL database as the user db_manager to test access to the hospital database.
Enter the database user password when prompted and press Enter to access the database.
Create a new sample doctors table.
The above SQL statement creates a doctors table in the hospital database with the following columns:
doctor_id as a PRIMARY KEY to uniquely identify each doctor.first_name and last_name for storing doctor names.appointment_date for storing the doctor's appointment date.SERIAL generates a unique doctor_id for each record.Insert sample data into the doctors table.
Query the doctors table to view all available records.
Output:
Exit the PostgreSQL console.
In this article, you installed PostgreSQL on your Ubuntu 22.04 server and used the psql utility to create databases and tables. You can now integrate PostgreSQL with your applications for secure database management. For more details, visit the PostgreSQL documentation.
0 Comments
Be the first to comment and share your perspective with the community.