
PostgreSQL is an open-source, advanced Relational Database Management System (RDBMS) designed for managing various data tasks. It uses Structured Query Language (SQL) to handle data in both small and large applications, including analytical systems, GIS, healthcare apps, and dynamic web applications.
This article walks you through installing PostgreSQL on an Ubuntu 20.04 server, enabling the database server, and securing it for production use.
Before you begin:
Have an Ubuntu 20.04 server.
Access the server using SSH as a non-root user with sudo privileges.
PostgreSQL is available in the default APT repositories on Ubuntu. Follow the steps below to install the PostgreSQL database server packages and enable the application to start at boot time.
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 operates using the default postgres privileged database user account. Follow the steps below to enable password authentication and secure the database server, ensuring that only authorized users can access the databases.
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.
The psql utility, pre-installed with the server package, allows you to access the PostgreSQL database console. You can also use compatible graphical tools for direct connection to the console. Follow the steps below to access your PostgreSQL database and create a new sample database for use with your 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 new table in the hospital database with the following columns:
doctor_id is a PRIMARY KEY that uniquely identifies each doctor in the doctors table.first_name and last_name 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 records.
Output:
Exit the PostgreSQL console.
PostgreSQL is now installed on your Ubuntu 20.04 server. You've accessed the database using the psql utility to create sample databases and tables. You can integrate PostgreSQL with your applications to securely manage database records. For more details, visit the official PostgreSQL documentation.
0 Comments
Be the first to comment and share your perspective with the community.