How to Install PostgreSQL in Linux

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re getting started with PostgreSQL, the first thing you’ll need to do is install a PostgreSQL server on your local machine. Fortunately, the installation process is a quick and easy one. While Postgres can be installed on a variety of operating systems, this tutorial will focus on the installation process for Linux. In this article, we’ll show you to how install PostgreSQL in Linux, walking you through each step of the process.

Prerequisites

If you plan to follow along with this tutorial, you’ll need to make sure that you have a Linux server running on your machine. Your Linux environment needs to be set up and configured properly in order to install PostgreSQL.

Display the PostgreSQL Package Version

Before we attempt to install the PostgreSQL server, we can display and view the package detail information using the show option seen below. This will list details such as version, file size, installed size, and section:

1
apt show PostgreSQL

Screenshot of the terminal show PostgreSQL

If you’re running certain Linux distributions such as Fedora, Red Hat or CentOS, you can also use the YUM repository to display your PostgreSQL package information. The command syntax is shown below:

1
yum info PostgreSQL-server

Installing PostgreSQL in Linux Mint

Linux Mint is a distribution based on Ubuntu and Debian that uses the APT-GET repository system. The default repositories include Postgres modules or packages– we can install them using the APT-GET packaging system.

We’ll need to build a source file that represents the correct PostgreSQL source for our particular distribution of Linux.

Update and Install PostgreSQL

Our next step is to update the package source of the system by executing the bash command shown below:

1
sudo apt-get update

We can then install the PostgreSQL database cluster using this command:

1
sudo apt-get install PostgreSQL PostgreSQL-contrib

NOTE: We also install the contrib package, which includes several additions that enhance the utility and functionality of PostgreSQL.

If you’re running a Linux version like Red Hat, Fedora or CentOS, you’ll need to use the yum or dnf package manager in conjunction with rpm. You can use the install option and include the name of the PostgreSQL package: postgreSQL-server. In order to acknowledge the questions posed automatically, we must also provide the -y flag, which indicates an automatic answer of yes:

1
sudo yum install -y PostgreSQL-server

The PostgreSQL Roles and Databases

By default, PostgreSQL uses the term “roles” to perform authentication and authorization. Roles are quite similar in function to standard Unix-style accounts, but Postgres doesn’t differentiate between users and classes; instead, it uses the broader term “role”.

During the installation process, a PostgreSQL user account will be created that is connected with the default PostgreSQL role. To access the PostgreSQL server on your device, execute the bash command:

1
sudo -u PostgreSQL psql

This will also open and access psql, the interactive terminal for PostgreSQL.

To create a role in PostgreSQL, simply execute the following command below:

1
CREATE USER some_user WITH CREATEDB CREATEROLE;

NOTE: You’ll need to grant a privilege to a user so that they can create a database in PostgreSQL.

At this point, we’ve reached the final step in the installation process. Let’s try to create a database in PostgreSQL with our new user as the owner:

1
CREATE DATABASE some_db WITH OWNER some_user;

If this command works as expected, you can confirm that you successfully installed PostgreSQL in Linux. You can now perform any database operations in your Postgres environment.

Conclusion

When you install PostgreSQL on your machine, it’s essential to proceed with care in order to avoid any setup or configuration errors. The steps involved in the process can vary depending on the Linux distribution you’re running, so it’s important to follow the correct instructions for your specific version of Linux. In this article, we showed you how to install PostgreSQL in Linux and explained each step of the process in detail. With our instructions to guide you, you’ll have no trouble installing Postgres in your own Linux environment.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.