PostgreSQL is a powerful, open-source object-relational database system that is widely used for web, mobile, geospatial, and analytics applications. In this tutorial, we will show you how to install PostgreSQL on Ubuntu, a popular Linux distribution.

PostgreSQL Docs: https://www.postgresql.org/docs/current/

Prerequisites

Before you begin, you should have a clean installation of Ubuntu and a user account with sudo privileges.

PostgreSQL Docs: https://www.postgresql.org/docs/

Step 1: Update the Package Manager Index

First, update the package manager index to ensure that you have access to the latest version of PostgreSQL:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt update
sudo apt update
sudo apt update

Step 2: Install the PostgreSQL Server Package

There are two option to install PostgreSQL server, One is using Ubuntu Repository and second is from the PostgreSQL Official Repository.

Method 1: Using Ubuntu Repository

Install the PostgreSQL server package and the postgresql-contrib package, which includes additional utilities and libraries:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo apt install postgresql postgresql-contrib
sudo apt install postgresql postgresql-contrib
sudo apt install postgresql postgresql-contrib

This will install PostgreSQL and create a new system user postgres.

Method 2: Using PostgreSQL Official Repository
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
sudo apt-get -y install postgresql
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql-12
# Create the file repository configuration: sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' # Import the repository signing key: wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - # Update the package lists: sudo apt-get update # Install the latest version of PostgreSQL. sudo apt-get -y install postgresql # If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql': sudo apt-get -y install postgresql-12
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
sudo apt-get -y install postgresql
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql-12

Also Read: How to create eks cluster using aws cli

Step 3: Starting and Verifying the PostgreSQL Service

Once the installation completes, run the below systemctl command to start the PostgreSQL service.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl start postgresql.service
sudo systemctl start postgresql.service
sudo systemctl start postgresql.service
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo systemctl status postgresql.service
sudo systemctl status postgresql.service
sudo systemctl status postgresql.service

Step 4: Connecting to the PostgreSQL server

By default, PostgreSQL creates a user postgres with the role postgres. To connect to the database, run the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo -u postgres psql
sudo -u postgres psql
sudo -u postgres psql
postgresql shell
PostgreSQL shell

Step 5: Creating and Deleting a PostgreSQL Database

After log in to the database you can perform database actions. To create database, run the following command:

Create Database

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
create database something_cool;
create database something_cool;
create database something_cool;
postgresql create database
create postgreSQL database

List Database

To list databases present in PostgreSQL server, run the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
\l
\l
\l
postgres list databases
List PostgreSQL databases

Delete Database

To delete database in PostgreSQL server, run the following command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
drop database something_cool;
drop database something_cool;
drop database something_cool;
postgresql drop database
Delete database from postgreSQL server
Conclusion

In this tutorial, we learned how to install PostgreSQL on Ubuntu and create and delete a database. You can now use PostgreSQL to store and manage your data. If you have any questions or comments, please let me know in the comments section below.

0 0 votes
Article Rating
Subscribe
Notify of
guest


0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments