Linux MintUbuntu Based

How To Install PostgreSQL on Linux Mint 21

Install PostgreSQL on Linux Mint 21

In this tutorial, we will show you how to install PostgreSQL on Linux Mint 21. For those of you who didn’t know, PostgreSQL, often fondly referred to as Postgres, is an open-source relational database management system known for its robust features and extensibility. In this comprehensive guide, we’ll walk you through the process of installing and configuring PostgreSQL on a Linux Mint 21 system using the Command Line Interface (CLI).

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of PostgreSQL on a Linux Mint 21.2 (Victoria).

Prerequisites

  • A server running one of the following operating systems: Linux Mint 21.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • While we’ll guide you through the process, a basic understanding of the command line will be beneficial. If you’re new to the CLI, you might want to acquaint yourself with some fundamental commands.
  • An active internet connection.
  • Administrative privileges are essential for installing and configuring software on your system. Ensure that you have superuser or sudo access.

Install PostgreSQL on Linux Mint 21

Step 1. Before any major installation, it’s a good practice to update your system’s package list and upgrade the installed packages. Open your terminal and execute the following commands:

sudo apt update
sudo apt upgrade

This ensures that you have the latest software packages and security updates installed on your system.

Step 2. Installing PostgreSQL on Linux Mint 21.

PostgreSQL maintains official repositories for various Linux distributions, including Linux Mint. To add the PostgreSQL repository and import the repository signing key, run the following commands:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Now that you’ve added the PostgreSQL repository, it’s time to install PostgreSQL and some additional packages. Execute the following command:

sudo apt update
sudo apt install postgresql postgresql-contrib

This command installs the PostgreSQL server and the contrib package, which includes various PostgreSQL extensions.

After the installation is complete, you need to start and enable the PostgreSQL service to ensure it starts automatically at boot. Execute these commands:

sudo systemctl start postgresql
sudo systemctl enable postgresql

To ensure that PostgreSQL is up and running, you can check its status using the following command:

sudo systemctl status postgresql

Step 3. Configuring PostgreSQL.

The PostgreSQL superuser is a pivotal role in managing the database. To set a secure password for the superuser (usually named ‘postgres‘), run this command:

sudo passwd postgres

In PostgreSQL, creating a new user is a common task. To create a new user, you can use the createuser command. For instance, if you want to create a user named ‘myuser,’ run the following:

sudo -u postgres createuser myuser

Once you have a user, you can proceed to create a new database. To create a database, use the createdb command:

sudo -u postgres createdb mydatabase

To grant specific privileges to your newly created user for the database, you’ll need to access the PostgreSQL interactive terminal (psql). Use the following command:

sudo -u postgres psql

Inside the psql shell, grant privileges as follows:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;

This command grants full privileges on ‘mydatabase‘ to ‘myuser.’ Adjust the privileges and roles as needed for your use case.

By default, PostgreSQL allows connections only from the local system. If you need to access the PostgreSQL database from remote systems, you’ll need to configure it to allow remote connections:

sudo nano /etc/postgresql/{version}/main/postgresql.conf

In the postgresql.conf file, look for the following line:

#listen_addresses = 'localhost'

Uncomment it and set it to ‘localhost’ or ‘*’ to allow connections from any IP address:

listen_addresses = '*'

Step 4. Using PostgreSQL.

With PostgreSQL installed and configured, you’re now ready to make the most of this powerful database management system. To interact with your PostgreSQL database, you can use the PostgreSQL interactive terminal, ‘psql.’ Simply run:

sudo -u postgres psql

This opens the psql shell where you can execute SQL commands and manage your database.

Step 5. Basic psql Commands for Database Management.

Here are some fundamental psql commands to help you manage your PostgreSQL database:

  • \l: List all available databases.
  • \c mydatabase: Connect to a specific database.
  • \dt: List all tables in the current database.
  • \du: List all database roles.
  • \q: Quit the psql shell.

These are just the basics. PostgreSQL offers a rich set of SQL commands for tasks like creating tables, inserting data, and querying databases.

Congratulations! You have successfully installed PostgreSQL. Thanks for using this tutorial for installing the latest version of PostgreSQL on the Linux Mint system. For additional help or useful information, we recommend you check the official PostgreSQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button