How To Install PostgreSQL 11 / 10 on Debian 9 / Debian 8 – Debian
PostgreSQL (simply known as Postgres) is an object-relational database management system (ORDBMS), available for many platforms including Linux, Microsoft Windows, FreeBSD, HP-UX, AIX, 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.
In this post, we will go through the installation of PostgreSQL 11 / 10 on Debian 9 / Debian 8.
Add PostgreSQL repository
PostgreSQL publishes deb packages for Debian based platforms, 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 information to /etc/apt/sources.list.d/postgresql.list file.
### Debian 9 ### echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list ### Debian 8 ### echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list
Install PostgreSQL
Once you have added the PostgreSQL repository run the following command to update the repository index.
sudo apt-get update
Install PostgreSQL using apt-get command.
### PostgreSQL 11 ### sudo apt install -y postgresql-11 ### PostgreSQL 10 ### 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 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 Sat 2019-02-23 01:49:54 EST; 23s ago Main PID: 2586 (code=exited, status=0/SUCCESS) CGroup: /system.slice/postgresql.service Feb 23 01:49:54 server systemd[1]: Starting PostgreSQL RDBMS... Feb 23 01:49:54 server systemd[1]: Started PostgreSQL RDBMS.
Configure PostgreSQL Server
PostgreSQL listens on loopback adapter (127.0.0.1) only. Sometimes the application hosted on external may need to connect to the database. For that, we need to configure PostgreSQL to listen on all adaptors.
### PostgreSQL 11 ### sudo nano /etc/postgresql/11/main/postgresql.conf ### PostgreSQL 10 ### sudo nano /etc/postgresql/10/main/postgresql.conf
Set the listen_addresses
to *
or
.
listen_addresses="*"
Restart PostgreSQL service.
sudo systemctl restart postgresql
Confirm the PostgreSQL listening on port 5432 using netstat command.
sudo netstat -antup | grep 5432
Output:
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 3712/postgres tcp6 0 0 :::5432 :::* LISTEN 3712/postgres
READ: netstat command not found on Ubuntu – Quick Fix
Access PostgreSQL
To manage the database, you need to log in as postgres user.
sudo su -l postgres
Access the database using the psql command. It is an interactive shell for working with PostgreSQL database.
psql
Output:
postgres@server:~$ psql
psql (11.2 (Debian 11.2-1.pgdg90+1))
Type "help" for help.
postgres=#
Secure PostgreSQL database
We will set a password for Linux user (postgres) as well as DB administrator (postgres).
Set password for Linux user (postgres):
sudo passwd postgres
Output:
raj@mydebian:~$ 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 the database admin password.
postgres=# password
OR
postgres=# password postgres
Output:
postgres=# password postgres Enter new password: Enter it again:
PostgreSQL DB Commands
The syntax for creating a database is:
$ su - postgres $ createdb test
The syntax for creating a user (raj) is:
$ psql test test=# CREATE USER raj WITH SUPERUSER LOGIN PASSWORD 'password';
Check whether the new 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 (11.2 (Debian 11.2-1.pgdg90+1)) 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 the PostgreSQL 11 / 10 on Debian 9 / Debian 8.