# Base install and setup PostgreSQL (Debian 8, Ubuntu 16.04)

Source: https://www.hukot.net/community/en/tutorials/base-install-and-setup-postgresql-debian-ubuntu-23
Updated: 2026-11-26

**PostgreSQL** is a powerful, open source object-relational database system. It has more than 20 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.

## 1. Install packages

```bash
apt-get install postgresql postgresql-client
```

## 2. Create a user

Switch to the `postgres` Linux user and create the database user:

```bash
su - postgres
createuser user1
```

Still as the `postgres` user, connect to the default `template1` database and set a password for the new user:

```bash
psql template1
```

```sql
ALTER USER user1 PASSWORD 'password';
```

Leave the `psql` console with `\q`, then run `exit` to get back to the root user.

That’s actually all you need to install and create the first user. Follow the remaining steps to allow remote access to the PostgreSQL server.

## 3. Enable TCP/IP connections

To let a remote client connect to the server, edit this line in the main config file `/etc/postgresql/<version>/main/postgresql.conf` (and remove the `#` at its start):

```
listen_addresses = '*'
```

Find the version number in the path with `ls /etc/postgresql`.

## 4. Allow connections from your IP address or range

Add this line to `/etc/postgresql/<version>/main/pg_hba.conf`:

```
host all all 46.36.0.0/16 md5
```

*Using this line I enabled the 46.36.0.0/16 IP range (netmask 255.255.0.0) to connect to the server. Make sure you allow as few IP addresses as possible.*

> [!WARNING]
> Don’t use the `trust` method instead of `md5`. It would let anyone from the allowed range into the database without a password, as any user including the `postgres` superuser.

## 5. Restart PostgreSQL

Changes in `postgresql.conf` take effect only after a restart:

```bash
systemctl restart postgresql
```

You’re all set. Make sure port 5432 is open in all firewalls to be able to manage the server remotely. To back up the database, see [How to export PostgreSQL database](tutorial:164).
