In my case, I needed to connect to PostgreSQL from inside its docker container.
So I opened the terminal of the docker container:
docker exec -it deep-postgres bash
Then I changed the user to postgres:
su postgres
And then I started the client:
psql
It all should look like this:
deep@deep:~$ docker exec -it deep-postgres bash
root@7b02446de835:/# su postgres
postgres@7b02446de835:/$ psql
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
psql (12.11 (Debian 12.11-1.pgdg110+1))
Type "help" for help.
postgres=#
Then, if you need to be able to log in with another user, you can create database and user like this:
CREATE database db;
CREATE USER dave WITH encrypted password 'test';
GRANT ALL privileges ON database db TO dave;
Then exit from psql and login using another user:
psql -U dave -d db
It will look like this:
postgres@7b02446de835:/$ psql -U dave -d db
DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
psql (12.11 (Debian 12.11-1.pgdg110+1))
Type "help" for help.
db=>