Install PostgreSQL on Ubuntu 18.04

Sylvia
4 min readApr 22, 2021

¡Noted: Self-taught notes. 學習筆記! <Review actually>

PostgreSQL is available in all Ubuntu version by default, however, there are two ways to install PostgreSQL on Ubuntu[1], to install PostgreSQL from PostgreSQL Apt Repository or from local Ubuntu repository. The biggest difference is PostgreSQL in Ubuntu by default won’t guarantee you that the system will automatically update when new release come out. Because the local repository only has “snapshots” of a specific version. The best practice is to install the software from the PostgreSQL Apt Repository. And I practice this way with AWS EC2 Ubuntu 18.04 (free tier), cos’ I wanna play around postgreSQL with CML (Though I did establish one whole Ubuntu local machine at work.) By the way, AWS provide the RDS service which you can create database in one minutes.

1: From PostgreSQL Apt Repository, to install PostgreSQL

This is a comparably complicated to install PostgreSQL, but it is fun.

Step 1: Add PostgreSQL Repository

To install from the official repository, you first need to add it to your system.

Import the GPG repository key with the commands:

$ sudo apt-get install wget ca-certificates$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

To add the PostgreSQL repository:

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
Step 1: Add PostgreSQL Repository

Step 2: Update the Package List

To make sure that the package list is up to date and to ensure that you will have installed the latest PostgreSQL package.

$ sudo apt-get update

Step 3: Install PostgreSQL

To install PostgreSQL and the PostgreSQL contrib package (which provide additional features):

$ sudo apt-get install postgresql postgresql-contrib

Also you can install PgAdmin4 if you want, the command line will be:

$ sudo apt-get install postgresql postgresql-contrib pgadmin4
$ sudo apt-get install postgresql postgresql-contrib pgadmin4
Installation of PostgreSQL DONE

Step 4: DONE!

Now you can check PostgreSQL version or other information, connect to it, or play around with it!

2: From Local Ubuntu Repository, to install PostgreSQL

This is the easy way install PostgreSQL, but it is not the point. The point is this way may not lead you to the latest version of the package.

Step 1: Check Available PostgreSQL Version

Before you decide whether you want to set up PostgreSQL from the Ubuntu repository, highly recommendation that to verify which versions are available. So update the repository first:

$ sudo apt update

then run the command:

$ apt show postgresql

It will show you that all the necessary information about the package, including the release number and size.

Step 2: Install PostgreSQL Package

If you are okay with the accessible PostgreSQL version from the local repository, use the following command to install the package:

$ sudo apt install postgresql postgresql-contrib

Step 3: DONE!

As simple as the steps! Now you have installed the PostgreSQL on your Ubuntu. You can go to check PostgreSQL version or other information, connect to it, or play around with it!

Time to Play

PostgreSQL creates a default user — postgres once you successfully installed the database. And this postgres user account has the default ‘postgres’ role. We can log in with it and start to have some fun.

Connect to PostgreSQL

$ sudo su - postgres
#or
$ sudo -i -u postgres

Then

$ psql

or you can simply type

$ sudo -u postgres psql

Both work the same.

Wanna know more about psql?

Check Connection Information

$ \conninfo

Quit PostgreSQL CML interface

By typing

$ Ctrl + d
#or
$ \q

Reference

[1] How to Install PostgreSQL on Ubuntu 18.04 -> https://phoenixnap.com/kb/how-to-install-postgresql-on-ubuntu

[2] Ubuntu Linux 18.04 安裝與使用 PostgreSQL 資料庫教學 -> https://blog.gtwang.org/linux/how-to-install-and-use-postgresql-ubuntu-18-04/

[3] [VPS] Ubuntu 上安裝 PostgreSQL 10 -> https://www.mxp.tw/8153/

— The End —

Published on 2021/04/22 & Last edited on 2021/04/22

--

--