Configure PostgreSQL

Check the connections allowed:

/etc/postgresql/12/main/postgresql.conf:
----------------------------------------
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)

Check the authentication allowed:

/etc/postgresql/12/main/pg_hba.conf:
------------------------------------
[...]
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

Create the database

First we will create the nextcloud user:

_$: sudo su - postgres
postgres_$: createuser -P nextcloud
Enter password for new role: <password>
Enter it again: <password>

And then the nextcloud database. The database owner will be the nextcloud user:

postgres_$: createdb --owner=nextcloud nextcloud

Let us check everything is alright:

postgres_$: psql
psql (12.17 (Ubuntu 12.17-0ubuntu0.20.04.1))

postgres=# \pset pager off
Pager usage is off.

postgres=# \x auto
Expanded display is used automatically.

postgres=# \l
List of databases
-[ RECORD 1 ]-----+----------------------
Name              | nextcloud
Owner             | nextcloud
Encoding          | UTF8
Collate           | en_GB.UTF-8
Ctype             | en_GB.UTF-8
Access privileges |

postgres=# \q