DEV Community

Faybeth Robina
Faybeth Robina

Posted on

STEP BY STEP GUIDE TO POSTGRESQL INSTALLATION AND SETUP USING A LINUX SERVER.

INTRODUCTION
PostgreSQL, frequently abbreviated as Postgres, is one of the most advanced open-source relational database management system available these days. It is world wide known and used because of its support for advanced data type and extensibility. With internet connection, terminal access, simple commands and a linux system one will be able to install and set up Postgres on a linux server. This article will guide you through each step to ensure your system is set up correctly for PostgreSQL usage.

Step 1:Update System Package.
Ensuring your system package is up to date is important before installation, this will help to prevent potential conflict and also help you get the latest version of PostgreSQL. You can update the system package with the following command:
sudo apt update

Step 2: Installing PostgreSQL.
The next step is to install PostgreSQL and its dependencies. You will use the command below:
sudo apt install postgresql postgresql-contrib

Step 3: Initializing the Database.
The database cluster needs to be initialized. After the installation is complete PostgreSQL service will start automatically.

Step 4:Starting the PostgreSQL Service.
After initialization, you need to check the status of PostgreSQL service using the command :
sudo system status postgresql
If you do not see "active (running)" in green then input the command:
sudo systemctl start postgresql

Step 5: Verifying the Installation.
To ensure that the PostgreSQL is installed correctly you verify the server status as shown below.

Verified status command, raw `sudo systemctl status postgresql` endraw

Step 6: Accessing PostgreSQL.
During installation PostgreSQL creates a system user called postgres. This user can be used to manage the database. You switch to the postgres user with:
sudo -i -u postgres

postgres
as shown above once switched, access the PostgreSQL prompt with:
psql

Step 7: Create a New User and Database
To create a new user and database you can run the following commands:

CREATE USER your_username WITH PASSWORD'your_password';
CREATE DATABASE your_database_name;
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;
Enter fullscreen mode Exit fullscreen mode

Step 8: Configuring PostgreSQL to Allow Remote Connections
PostgreSQL is configured to listen only on the localhost. To allow remote connections you modify the configuration in the postgresql. conf file and the pg_hba.conf file. First, open the postgresql. conf file with the command sudo nano/etc/postgresql/<version>/main/postgresql.conf, then edit the line # listen_addresses = 'localhost' to listen_addresses = '*'. This change allows PostgreSQL to listen on all IP addresses. Next, edit the pg_hba.conf file to add a line that permits connections from remote IP adressses.

sudo nano/etc/postgresql /<version>/main/pg_hba.conf
       host    all     all  
   0.0.0.0/0           md5
Enter fullscreen mode Exit fullscreen mode

These configurations allow all users from any IP address to connect to all database. Finally ,restart PostgreSQL to apply the changes by running the code:
sudo systemctl restart postgresql

You can now start using PostgreSQL on your Ubuntu system. You can connect to the PostgreSQL database using variety of tools like PgAdmin or DBeaver.

connecting PostgreSQL database to DBeaver
The image above shows how to connect PostgreSQL database to DBeaver where
Host: your server's IP address
Port: 5432
Username: your database user your_username
Password: password you created (your_password)
Database:your_database_name

Conclusion
Following the steps above you can install and set up PostgreSQL on your linux server. You can start utilizing its powerful features for your database needs. PostgreSQL can be used by both small and large scale applications.

Top comments (0)