How to access the Discourse's database?

Discourse usually works in a virtual machine (Ubuntu) inside a container (Docker in production and Vagrant in development).
So you can not access Discourse database by the usual way: your host operating system knows nothing about a software inside the container.

Firstly you need to create ssh tunnel (port forwarding): link PostgreSQL port (5432) inside the virtual machine with a port on your host machine.

If you are using Docker it can be achieved by rule in app.yml:

expose:
  <standard definitions>
  - "15432:5432" # PostgreSQL

Here we link host port 15432 with guest (virtual machine’s) port 5432.

If you are using Vagrant it can be achieved by command:

vagrant ssh -- -L 15432:localhost:5432

Here we link host port 15432 with guest (virtual machine’s) port 5432.

Then, you need install and configure PostgreSQL client software.
I recommend phpPgAdmin to access Discourse database in Docker and pgAdmin to access Discourse database in a local Vagrant environment.

Vagrant PostgreSQL login is «discourse» and there is no password.
Docker PostgreSQL login is «postgres» and there is no password too, but you must specify it (phpPgAdmin will not allow you login without password). To setup password:

./launcher enter app
su - postgres
psql
ALTER ROLE postgres WITH PASSWORD '<your password>';

Discourse database in phpPgAdmin:

Discourse database in pgAdmin: