# How to install and use PostgreSQL on Ubuntu 20.04?

{% embed url="<https://youtu.be/BdadmYj3dV0>" %}

### Overview

Many websites and apps use relational database management systems (RDBMS) as a fundamental building block. They offer a systematic technique to gather, arrange, and access data.

A RDBMS called **PostgreSQL**, sometimes known as **Postgres**, offers an implementation of the SQL. It complies with standards and includes several cutting-edge capabilities, like concurrency without read locks and transactions that can be trusted.

Learn step-by-step how to install and use PostgreSQL on Ubuntu 20.04 with this easy-to-follow guide. Start using this powerful open-source database today!&#x20;

### **Prerequisites**

There are certain prerequisites that need to be met before you begin:

* Ubuntu 20.04 server
* SSH supported code editor
* Basic knowledge of database

### Get Started

Postgres packages by default present in ubuntu's repositories, so we will use *apt* packaging system to install it. Follow the below steps:

**Step 1:** Update server's local package index

```
sudo apt update
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/646_2023-01-03-11_25_26-.png" alt=""><figcaption></figcaption></figure>

**Step 2:** Install Postgres packages with -contrib package.

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

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/647_2023-01-03-11_39_25-Puneet-Agarwal-You-_-Microsoft-Teams.png" alt=""><figcaption></figcaption></figure>

**Step 3:** Check if the server is working properly using *systemct1 start* command.

```
sudo systemctl start postgresql.service
```

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/648_2023-01-03-11_43_59-Puneet-Agarwal-You-_-Microsoft-Teams.png)

Now, the PostgreSQL is installed, let's see how it works.

**Moving to Postgres Account**

**Step 1:** On your server, change to the Postgres account.

```
sudo -i -u postgres
```

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/650_2023-01-03-11_46_06-Puneet-Agarwal-You-_-Microsoft-Teams.png)

**Step 2:** Access the PostgreSQL prompt using the following command.

```
psql
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/651_2023-01-03-11_48_30-Puneet-Agarwal-You-_-Microsoft-Teams.png" alt=""><figcaption></figcaption></figure>

**Step 3:** Exit the PostgreSQL prompt using:

```
postgres=# \q
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/656_2023-01-03-11_57_32-Puneet-Agarwal-You-_-Microsoft-Teams.png" alt=""><figcaption></figcaption></figure>

**New role creation**

Create a new role by typing:

```
createuser --interactive
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/658_2023-01-03-12_03_20-Puneet-Agarwal-You-_-Microsoft-Teams.png" alt=""><figcaption></figcaption></figure>

**New Database creation**

Type the following command to create a new database:

```
createdb user2
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/670_2023-01-03-13_16_33-.png" alt=""><figcaption></figcaption></figure>

**Connecting to a database**

Use the following command to connect to database:

```
psql -d 'database name'
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/671_2023-01-03-13_20_15-.png" alt=""><figcaption></figcaption></figure>

**Creating and deleting Table**

Syntex for creating a table:

```
CREATE TABLE 'Name of the table' (
    column_1 type (length) col_constraints,
    column_2 type (length),
    column_3 type (length)
);
```

```
For example:
CREATE TABLE check_price ( 
  id serial PRIMARY KEY,
  name VARCHAR (30),
  description VARCHAR (30),
  price numeric CHECK (price > 0)
  );
```

```

To view the table type:
```

```
\d
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/673_2023-01-03-13_36_04-.png" alt=""><figcaption></figcaption></figure>

**Adding data in a table**

```
INSERT INTO check_price (id, name, description) VALUES ('1', 'Jack', 'Test');
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/674_2023-01-03-13_42_13-.png" alt=""><figcaption></figcaption></figure>

**To view this data, type**

```
SELECT * FROM check_price;
```

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/675_2023-01-03-13_43_57-.png)

**Delete data from table**

```
DELETE FROM check_price WHERE id = '1';
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/676_2023-01-03-13_45_50-.png" alt=""><figcaption></figcaption></figure>

**View table again**

```
SELECT * FROM check_price;
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/677_2023-01-03-13_48_03-155.130.70.250.png" alt=""><figcaption></figcaption></figure>

### **Conclusion**

PostgreSQL is now configured on your Ubuntu 20.04 server.
