Step-by-step guide: How to Install PostgreSQL 12 on Ubuntu with Real-Life Code Examples that Will Make Your Database Management a Breeze.

Table of content

  1. Introduction
  2. Prerequisites
  3. Update Packages
  4. Install PostgreSQL
  5. Connect to PostgreSQL
  6. Create a Database
  7. Create a Table and Insert Data
  8. Conclusion


Are you looking to streamline your database management with PostgreSQL 12 on Ubuntu? Look no further than this step-by-step guide. With real-life code examples, you'll be able to follow along and install PostgreSQL 12 on Ubuntu with ease.

PostgreSQL 12 offers significant improvements over previous versions, including better performance and enhanced security features. And with Ubuntu’s user-friendly interface, you'll be able to manage your PostgreSQL databases easily and efficiently.

In this guide, we'll cover everything from downloading and installing PostgreSQL 12 on Ubuntu to configuring your server, creating a database, and adding users. Even if you're new to PostgreSQL or Ubuntu, you'll find our clear and concise instructions easy to follow.

So let's get started and take your database management to the next level with PostgreSQL 12 on Ubuntu.


Before diving into the step-by-step guide for installing PostgreSQL 12 on Ubuntu, it's important to cover the you'll need to have in place.

First and foremost, you'll need access to a Ubuntu machine. PostgreSQL is compatible with a wide range of operating systems, but this tutorial specifically focuses on Ubuntu.

You'll also need to have access to the internet on this machine, as we'll be downloading and installing PostgreSQL directly from the web.

Finally, it's helpful to have some basic knowledge of the command line and Ubuntu's package manager, apt-get. But don't worry if you're a beginner – we'll explain everything in detail as we go.

With these in place, you're ready to start the installation process!

Update Packages

Before diving into the installation process, it is important to ensure that the system has the latest updates installed. This will not only ensure that the system is stable, but will also provide the necessary components for PostgreSQL to function optimally. To , the apt package manager can be used by running the following command:

sudo apt update && sudo apt upgrade -y

This command updates the package list and installs the latest versions of packages that have newer versions available. The "-y" flag automatically confirms the installation of any updates without requiring a prompt, which is useful for server installations.

It is important to note that this command may take a while to complete, depending on the number of packages that need to be updated. Once the command has finished running, the system is ready for PostgreSQL installation.

Updating packages is a crucial step in the installation process since it ensures the system has all the necessary components to support PostgreSQL. Skipping this step can result in system instability and may cause issues when installing PostgreSQL. Therefore, it is essential to run this command before proceeding with the installation process.

Now that the system is up to date, it's time to move on to the installation of PostgreSQL. Get ready to take your database management to the next level!

Install PostgreSQL

Installing PostgreSQL

PostgreSQL is an open-source relational database management system that is widely used for storing and managing data. To 12 on Ubuntu, follow these simple steps:

  1. Update your system – Before installing any new package, it is always a good idea to update your system. Open the terminal and run the following command:
sudo apt-get update
    • Once your system is up-to-date, you can by running the following command:
sudo apt-get -12

This command will version 12 on your Ubuntu machine. The installation may take some time, depending on your system's speed and internet connection.

  1. Verify the installation – After the installation is complete, you can verify if PostgreSQL is running on your system by checking its status using the following command:
sudo systemctl status postgresql

This command will display the status of the PostgreSQL service running on your system. If the service is running, you will see a message that says "active (running)."

Congratulations! You have successfully installed PostgreSQL 12 on your Ubuntu system. To start using it, you can create a new database, create a new user, and start storing and managing your data.

Now that you have PostgreSQL up and running on your Ubuntu machine, it's time to dig deeper and explore all of its powerful features. So, let's get started!

Connect to PostgreSQL

Now that PostgreSQL is installed on your Ubuntu system, it's time to connect to it and start using it!

To connect to the PostgreSQL server, you first need to have the psql command-line tool installed on your system. This tool allows you to interact with the PostgreSQL server and execute SQL queries from the terminal.

To install the psql tool, run the following command:

sudo apt-get install postgresql-client-12

Once the tool is installed, you can connect to the PostgreSQL server by running the following command:

sudo -u postgres psql

This will connect you to the PostgreSQL server as the postgres user, which is the default user for the server.

From here, you can start executing SQL queries and managing your databases. For example, you can create a new database by running the following command:


And you can switch to this database by running the following command:

\c mydatabase;

Now you're ready to start managing your databases using PostgreSQL!

Congratulations! You've successfully installed PostgreSQL 12 on your Ubuntu system and learned how to connect to the server. With this powerful tool at your fingertips, you can now start managing your databases with ease. What are you waiting for? Start exploring and see what you can do with PostgreSQL!

Create a Database

Creating a database in PostgreSQL 12 is a simple yet crucial step in managing your data. To , start by opening the psql prompt by typing "sudo -u postgres psql" in the terminal. Once in the prompt, type "CREATE DATABASE databasename;" and hit enter. Congratulations, your database has been created!

To confirm that your database exists, type " \l" in the psql prompt to display all available databases. You should see your newly created database listed there. Now that your database exists, you can start adding tables, columns, and data to it.

But before you do that, make sure to set a password for your newly created database by typing "ALTER USER postgres PASSWORD 'newpassword';" This will ensure that only authorized personnel can access and manipulate your database.

By following these simple steps, you can create and secure your PostgreSQL 12 database. Now it's time to start exploring and utilizing all the powerful features that PostgreSQL 12 has to offer! So, what are you waiting for? Go ahead and create your first database and start managing your data with ease!

Create a Table and Insert Data

To create a table in PostgreSQL, we first need to connect to a database within our PostgreSQL server using the command line or a GUI client. Once connected, we can use the CREATE TABLE statement to specify the table's name and columns, along with their data types and any constraints. For example, we could create a simple table called "employees" with columns for their name, age, and job title like this:

CREATE TABLE employees (
   name VARCHAR(50) NOT NULL,
   age INTEGER CHECK (age > 0),
   job_title VARCHAR(50)

Here, we've specified that the "name" column must contain a non-null string value of up to 50 characters, the "age" column must be an integer greater than zero, and the "job_title" column is optional and can also contain up to 50 characters.

Once we have our table created, we can start inserting data into it using the INSERT statement. This statement requires us to specify which table we want to insert into and the values for each column we want to add. For example, let's say we want to add a new employee named "John Smith" who is 35 years old and works as a "Software Engineer":

INSERT INTO employees (name, age, job_title)
VALUES ('John Smith', 35, 'Software Engineer');

This will add a new row to our "employees" table with the values we specified. We can continue to add more employees in the same way, and even update or delete existing ones using the UPDATE and DELETE statements.

With these simple code examples, managing a PostgreSQL database becomes a breeze. The possibilities are endless, and it's exciting to see what you can achieve with your PostgreSQL installation. So why not give it a try and start experimenting with your own tables and data?


In , installing PostgreSQL 12 on Ubuntu is a straightforward process that can be easily accomplished using the steps outlined in this guide. With the use of real-life code examples, managing your database has never been easier.

We encourage readers to give PostgreSQL a try and experience the power of this powerful relational database management system. It offers features such as data replication, high availability, and scalability, making it a favorite among developers and businesses alike.

So don't wait any longer, follow our step-by-step guide and start your PostgreSQL 12 installation with confidence. Happy managing!

As a senior DevOps Engineer, I possess extensive experience in cloud-native technologies. With my knowledge of the latest DevOps tools and technologies, I can assist your organization in growing and thriving. I am passionate about learning about modern technologies on a daily basis. My area of expertise includes, but is not limited to, Linux, Solaris, and Windows Servers, as well as Docker, K8s (AKS), Jenkins, Azure DevOps, AWS, Azure, Git, GitHub, Terraform, Ansible, Prometheus, Grafana, and Bash.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top