How To Install PostgreSQL 10 on Ubuntu 18.04 LTS

0

PostgreSQL (aka Postgres) is an object-relational database management system (ORDBMS), available for Linux, Microsoft Windows, FreeBSD, OpenBSD, Solaris, and Mac OS.

PostgreSQL is released under the PostgreSQL License, a free and open source permissive software license, developed by the PGDG (PostgreSQL Global Development Group), a group of individual volunteers and corporate companies.

Here, we will go through steps on how to install PostgreSQL 10 on Ubuntu 18.04.

Add PostgreSQL repository

PostgreSQL publishes deb packages for Ubuntu, and their packages are fresher than those available in OS base repository.

Import the PostgreSQL signing key.

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | sudo apt-key add -

Add the PostgreSQL repository to /etc/apt/sources.list.d/postgresql.list file.

echo "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

Install PostgreSQL

Execute the below command to update the repository index.

sudo apt update

Output:

Install PostgreSQL 10 on Ubuntu 18.04 - Update Repository Information
Install PostgreSQL 10 on Ubuntu 18.04 – Update Repository Information

Install the PostgreSQL 10 package using the apt command.

sudo apt install -y postgresql-10

Control PostgreSQL service

To start PostgreSQL service, run:

sudo systemctl start postgresql

To stop PostgreSQL service, run:

sudo systemctl stop postgresql

To restart PostgreSQL service, run:

sudo systemctl restart postgresql

To check the running status of PostgreSQL service, run:

sudo systemctl status postgresql

Output:

 postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Tue 2018-05-22 11:28:16 UTC; 2min 51s ago
 Main PID: 2830 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 2322)
   CGroup: /system.slice/postgresql.service

May 22 11:28:16 server systemd[1]: Starting PostgreSQL RDBMS...
May 22 11:28:16 server systemd[1]: Started PostgreSQL RDBMS.

Access PostgreSQL

To manage the database, you need to log in as the postgres user.

sudo su -l postgres

Use the psql, an interactive shell for working with PostgreSQL database.

psql

Output:

postgres@server:~$ psql
psql (10.4 (Ubuntu 10.4-2.pgdg18.04+1))
Type "help" for help.

postgres=#

Secure PostgreSQL database

We will now set a password for Linux user (postgres) as well as DB administrator (postgres).

Set password for Linux user (postgres)

sudo passwd postgres

Output:

raj@server:~$ sudo passwd postgres
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Set password for DB administrator (postgres)

su - postgres
psql

On psql shell, run below command to change database admin password.

postgres=# \password

OR

postgres=# \password postgres

Output:

postgres=# \password postgres
Enter new password:
Enter it again:

Extras

The syntax for creating a database is:

$ su - postgres

$ createdb test

The syntax for creating a user is:

$ psql test

psql (10.4 (Ubuntu 10.4-2.pgdg18.04+1))
Type "help" for help.

test=# CREATE USER raj WITH SUPERUSER LOGIN PASSWORD 'password';

Check whether the user has been created or not.

test=# \du

Output:

test=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 raj       | Superuser                                                  | {}

Log in with the user “raj”.

psql -h localhost -d test -U raj

Output:

psql (9.6.5)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

test=#

The syntax for listing databases:

test=# \l

Output:

test=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

That’s all!. You have successfully installed PostgreSQL 10 on Ubuntu 18.04.

Further Reading

You might also like

How To Install PostgreSQL 10 on Ubuntu 18.04 LTS

0