---
# System prepended metadata

title: PostgreSQL 13
tags: [PostgreSQL]

---

# PostgreSQL 13

## Installation guide

### Linux Ubuntu 18.04 up

```shell=
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt install postgresql-13 postgresql-client-13
```
### Service Check

```shell=
$ systemctl status postgresql.service 
```
### Test PostgreSQL Connection And User role control 

change ubuntu user ro
```shell=
$ sudo su - postgres
```
If "postgres" user needs a remote connection, you must change the password.

```shell=
$ psql -c "alter user postgres with password 'xxxxxx'"
```
Start PostgreSQL prompt by using the command create Database & grant role

```bash=
CREATE DATABASE mytestdb;

## create user
CREATE USER mytestuser WITH ENCRYPTED PASSWORD 'MyStr0ngP@SS';

## grant role

GRANT ALL PRIVILEGES ON DATABASE mytestdb to mytestuser

```
[Alter Attributes](h[ttps://](https://chartio.com/resources/tutorials/how-to-change-a-user-to-superuser-in-postgresql/))
```
ALTER USER ${you_user} WITH SUPERUSER;
```

### Configure Remote Connection 


```bash=
$ sudo vim /etc/postgresql/13/main/postgresql.conf 
```

Find listen_addresses change for you listen specified ip or *

```bash=
# Listen on all interfaces
listen_addresses = '*'

# Listen on specified private IP address
listen_addresses = '192.168.10.11'
```

Also set PostgreSQL to accept remote connections from allowed hosts.
sample :
```bash=
# Accept from anywhere
host all all 0.0.0.0/0 md5

# Accept from trusted subnet
host all all 10.10.10.0/24 md5
```

Restart postgresql

```bash=
$ sudo systemctl restart postgresql
```


###### tags: `PostgreSQL`