
PostgreSQL is an open-source relational database management system (RDBMS) that supports Structured Query Language (SQL). The database server is suitable for storing data and running queries in a wide variety of information systems. PostgreSQL's most notable performance features include multi-version concurrency control, transaction support, foreign keys, user-defined data types, and custom views.
This article explains how to install and enable access control for the PostgreSQL database server on Debian 12.
Before you begin:
PostgreSQL is available in the default package repositories on Debian 12. Follow the steps below to install PostgreSQL using APT.
Install postgresql-common package to create the latest PostgreSQL version information.
The above package creates a new repository information script (/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh) containing the latest PostgreSQL version details.
Run the PostgreSQL repository setup script.
Output:
Press Enter to add the repository when prompted.
Update the package information index.
Display all the available PostgreSQL versions that you can install.
Output:
Based on the above output:
16 is available in the PostgreSQL repository.15 is available in the default Debian 12 repository.Install the PostgreSQL database server package.
The PostgreSQL database server runs under the postgresql system service. Follow the steps below to manage the PostgreSQL service using systemctl.
Enable the PostgreSQL service to automatically start at boot.
Start the PostgreSQL service.
View the PostgreSQL service status and verify that it's running.
Output:
Stop the PostgreSQL service.
Restart the PostgreSQL service.
By default, PostgreSQL creates an administrative postgres user. The user authenticates without a password using the Unix domain socket. Follow the steps below to enable password authentication to secure PostgreSQL:
Access the PostgreSQL database console as the user postgres.
Modify the user to use password authentication. Replace Secur3Passw0rd with a strong password.
Exit the database console.
Open the /etc/postgresql/16/main/pg_hba.conf main PostgreSQL configuration file using a text editor like nano.
Find the following configuration directive.
Edit the last column and change it from peer to scram-sha-256 to enable password authentication for the local postgres user.
Find the following directive for all PostgreSQL users.
Replace peer with scram-sha-256 to enable password authentication.
Save and close the file.
Restart PostgreSQL to apply the configuration changes.
Access the PostgreSQL console as postgres.
Enter the password you set earlier and press :key_enter when prompted.
Create a new db_admin database role and grant LOGIN and CREATEDB privileges to allow the user to log in to the database server and create databases.
PostgreSQL includes the psql CLI utility you can use to access the database console by default. You can also use graphical tools such as pgAdmin to manage the PostgreSQL databases. Follow the steps below to access the PostgreSQL database server using the psql utility:
Access the PostgreSQL database server as db_admin and enter the password you created earlier.
Create a sample school database.
Switch to the new database.
Enter db_admin role and press Enter when prompted.
Output:
Create a sample teachers table with 4 columns.
The above command creates a new table with the following column specifications:
staff_id: Uses the SERIAL data type and a PRIMARY KEY constraint.first_name and last_name: Use the CHARACTER VARYING data type to store variable length data with a limit of 100 characters and a NOT NULL constraint to restrict empty values.joining_date: Uses the DATE data type to store date data.Add sample data to the teachers table.
Query the teachers table to view the available records.
Output:
Exit the PostgreSQL console.
You installed PostgreSQL on Debian 12, created a sample database, and used psql to access the database server. You can deploy PostgreSQL in your web applications to store data, set up a data warehouse for data analytics, or handle geographical data. For more information, visit the PostgreSQL documentation.
0 Comments
Be the first to comment and share your perspective with the community.