
PostgreSQL is an open-source advanced Relational Database Management System (RDBMS) designed to handle a wide range of data management tasks. It supports the Structured Query Language (SQL) to manage data in small and large enterprise applications such as analytical systems, Geographic Information Systems (GIS), healthcare applications, and dynamic web applications.
This article explains how to install PostgreSQL on a FreeBSD 14.0 server. You will enable the PostgreSQL database server and secure it for production use on your server.
Before you begin:
Deploy a FreeBSD 14.0 server instance on Vultr.
Access the server using SSH.
Create a non-root user with sudo privileges and switch to the user.
PostgreSQL is available with multiple versions in the default pkg repository on FreeBSD. Follow the steps below to install the latest PostgreSQL packages on your server.
Update your server packages index.
List all available PostgreSQL packages.
Output:
PostgreSQL 16 is the latest version available in the pkg repositories. Install the client and server packages to enable PostgreSQL on your server.
Install the PostgreSQL client and server packages on your server.
PostgreSQL uses the postgresql system service profile to run on your FreeBSD server. Follow the steps below to enable the PostgreSQL database server to start automatically at system boot and verify the service status to manage the PostgreSQL processes on your server.
Enable the PostgreSQL service to automatically start at system boot.
Output:
Initialize the PostgreSQL database to enable the postgres user on your server.
Start the PostgreSQL database server.
View the PostgreSQL service status and verify that it's running.
Output:
PostgreSQL runs with the default postgres privileged database user on your server by default when the initialization process is complete. Follow the steps below to enable password authentication and secure the PostgreSQL database server to grant permitted users access to specific databases.
Switch to the postgres user account and log in to the PostgreSQL database server console.
Modify the default postgres user to use a new encrypted password. Replace strong_password with your desired password.
Create a new sample database user db_manager with a strong encrypted password.
Exit the PostgreSQL console.
Run the following command to change the default trust value to scram-sha-256 in the main PostgreSQL configuration file pg_hba.conf to enable password authentication on the PostgreSQL database server.
Restart the PostgreSQL database server to apply your configuration changes.
You can access the PostgreSQL database console using the psql utility that's pre-installed with the PostgreSQL client package on your server. In addition, you can access the PostgreSQL database server using compatible tools that create a direct connection to the database console. In the following steps, use the psql utility to access your PostgreSQL database server console and create a new sample database to use on your server.
Create a new database example-vultr and grant ownership privileges to the db_manager user.
When prompted, enter the postgres user password you created earlier.
Log in to the PostgreSQL database server using the sample user db_manager to test access to the example-vultr 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 example-vultr 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 a doctor's appointment date.SERIAL generates a new 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 database console.
You have installed PostgreSQL on your FreeBSD 14.0 server and accessed the database server console using the psql utility to create sample databases and table records. You can further integrate the PostgreSQL database server with your existing applications to securely enable the creation and management of database records on your server. For more information and configuration options, please visit the official PostgreSQL documentation.
0 Comments
Be the first to comment and share your perspective with the community.