# How To Install PostgreSQL on Ubuntu 22.04

PostgreSQL is one of the leading and most widely used open-source relational database management systems it is a robust and high-performance database system known for its flexibility in handling multiple data types stability, integrity, and concurrency.

## Prerequisites <a href="#prerequisites" id="prerequisites"></a>

Make sure you have the necessary prerequisites to successfully install Postgresql on ubuntu 22.04.

* Launch the [Ubuntu 22.04 instance](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms) on Neevcloud
* Access the server using [SSH](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms#ssh)

### Update && Upgrade the system <a href="#update-and-and-upgrade-the-system" id="update-and-and-upgrade-the-system"></a>

First, ensure your package list is up to date to avoid any issues with outdated packages

```
sudo apt-get update -y
sudo apt-get upgrade -y
```

## Install PostgreSQL <a href="#install-nginx" id="install-nginx"></a>

To install PostgreSQL use the `apt` package manage and run the following command from a terminal prompt:

```
sudo apt install postgresql postgresql-contrib -y
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fib4PcbKUEqIA6NakZTxJ%2Fimage.png?alt=media&#x26;token=eb0abffa-3fdf-42a8-a91e-a50a8bbb4c0b" alt=""><figcaption></figcaption></figure>

### Start and Enable PostgreSQL <a href="#start-and-enable-mariadb" id="start-and-enable-mariadb"></a>

After installation, the PostgreSQL service starts and enables

Once the PostgreSQL is installed, start the PostgreSQL server

Once the MariaDB is installed, start the MariaDB server

```
sudo systemctl start postgresql
```

Enable it to start when the system rebooted

```
sudo systemctl enable postgresql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FrY6LZ1YksEqj5NbueyPB%2Fimage.png?alt=media&#x26;token=3b7c59d1-3897-4d3d-99bd-f3b6480e98ed" alt=""><figcaption></figcaption></figure>

### To check the status <a href="#to-check-the-status" id="to-check-the-status"></a>

Verify that the database service is active and running. Run the following command

```
sudo systemctl status postgresql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FLfIZH0f91KhUPeMTPQHC%2Fimage.png?alt=media&#x26;token=33ed42b5-a262-4f6e-8e0f-9033cb1790d4" alt=""><figcaption></figcaption></figure>

### To check the version&#x20;

Verify the version installed by running the command:

```
sudo psql --version
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FaeGYULeWYhW8lfsoI2m4%2Fimage.png?alt=media&#x26;token=d896ef59-8b11-4060-83bc-e989043a4cb6" alt=""><figcaption></figcaption></figure>

### Manage PostgreSQL service

PostgreSQL runs quietly in the background upon installation. By default, it listens on TCP port 5432. You can verify this using the `ss` command.

```
sudo ss -antpl  | grep 5432
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FMm0rt0lWj3nbBBeZTSaB%2Fimage.png?alt=media&#x26;token=e37ed294-2ff6-4558-aee1-0c741d072f72" alt=""><figcaption></figcaption></figure>

## Access PostgreSQL Prompt

When PostgreSQL is installed, a standard user account named Postgres is automatically created. The Postgres user takes on the default Postgres role as well.

PostgreSQL uses a role-based authentication system as its default. Having superuser privileges, the user 'Postgres' has complete administrative control. Enter this command to log in as the user 'Postgres'

```
sudo su - postgres
```

Then, to access the PostgreSQL prompt, type

```
psql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FEuzbYxIVOb40J9T4qqKe%2Fimage.png?alt=media&#x26;token=44d47ff7-4d31-4c70-b34b-8674996d0862" alt=""><figcaption></figcaption></figure>

From here, you can start executing your database management tasks.

{% hint style="info" %}
To leave the prompt, type **`\q`**, and exiting the prompt will return you to your Postgres account in your terminal. Execute the **`exit`** command to return to your normal account and leave.
{% endhint %}

```
\q
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F0s9VTUuBlSJOGEhtmaf4%2Fimage.png?alt=media&#x26;token=7b3ff154-c7c9-4b0b-baa8-6111eeae2dbd" alt=""><figcaption></figcaption></figure>

```
exit
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F9T1bRHDXd00pvuE5D40T%2Fimage.png?alt=media&#x26;token=6a9053e5-63d1-44c8-a607-d7703040f6f9" alt=""><figcaption></figcaption></figure>

## Create a New Database

This section will cover the process of establishing a database in PostgreSQL and generating tables within it. The standard PostgreSQL installation includes three pre-existing databases: `Postgres`, `template0`, and `template1`.

To list the existing databases to run these commands.

```
\l
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fh6mI3fDMfK5JT84XtvV1%2Fimage.png?alt=media&#x26;token=fe1c2118-a205-47ca-b2dc-9b49d54f0f8d" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
To create a database, run the `create database database_name;` command
{% endhint %}

```
create database example_db;
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FP0MPAS3bmvleU9yG1Euy%2Fimage.png?alt=media&#x26;token=5936e49d-c527-4d36-845e-ac8f3b8682e2" alt=""><figcaption></figcaption></figure>

### Create a New Table

In order to make a new table and add data, you must first change to the desired database where the table will be stored. Use the **`\c`** command to switch the database.

```
\c 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FaATuF4v1imsu1RsB9pK9%2Fimage.png?alt=media&#x26;token=a33e26a5-4086-4e3e-bfbc-f536ef5ae94e" alt=""><figcaption></figcaption></figure>

After transitioning to the database, you can begin to create tables. The syntax for a table in SQL is as follows

In this instance, we will generate a table named employees that includes six specific columns

{% hint style="info" %}

```sql
CREATE TABLE employees (
	Person_ID serial PRIMARY KEY,
	First_Name VARCHAR ( 50 ) NOT NULL,
	Last_Name VARCHAR ( 50 ) NOT NULL,
	Email VARCHAR ( 255 ) UNIQUE NOT NULL,
           City VARCHAR ( 50 ) NOT NULL,
           age INT NOT NULL
);
```

{% endhint %}

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FuAjprR8cj6jgltqGyPAY%2Fimage.png?alt=media&#x26;token=3f205e20-1f70-4bfb-8656-6442b601ebb9" alt=""><figcaption></figcaption></figure>

To check the status of creating a table in a database to follow these **`\dt`** command

{% hint style="info" %}
You can also use the **`\d table_name`**` ``syntax to view the table schema.`
{% endhint %}

```
\dt
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fz2h0HDUlCrTgnTzWfAO2%2Fimage.png?alt=media&#x26;token=172034b7-5d9c-4ebb-bc81-f37bd88e9777" alt=""><figcaption></figcaption></figure>

To insert the values into the table, use the the **`INSERT INTO table_name`** command.

{% hint style="info" %}

```sql
INSERT INTO employees 
VALUES ( 001, 'Phil', 'Shaw', 'philshaw@gmail.com', 'New York', 40);
```

{% endhint %}

Now check the status of table records to run these **`SELECT  * FROM  table_name`** command

```
SELECT * FROM employees;
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FscW643PQHTSxbssuqNhY%2Fimage.png?alt=media&#x26;token=1c406374-7b14-4d95-84d9-9b63f22da3b1" alt=""><figcaption></figcaption></figure>
