How to Fix the “Role ‘Postgres’ Does Not Exist” Error When Deploying PostgreSQL Docker Container

ChunKang Wong
1 min readAug 25, 2023

--

TL; DR: First check port usage on port 5432 using command sudo lsof -i :5432, there should be only one process that says com.docke under COMMAND column. Terminate other processes using command sudo kill [pid].

There I was exploring PostgreSQL on Docker while also uninstalling the existing PostgreSQL on my macOS. Following is the compose.yaml that I used for deploying the postgres docker container:

services:
postgres:
container_name: postgres
image: postgres:latest
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=testdb
ports:
- "5432:5432"
restart: always

Somehow I just could not connect to this PostgreSQL instance from the outside. It kept returning the Role ‘Postgres' Does Not Exist error even though the role did absolutely exist.

If you were like me, who just uninstalled PostgreSQL from the main machine, make very sure that you uninstall it cleanly, including terminating any remaining processes.

One way to check is by using command: sudo lsof -i :5432. In my case, it returns three processes, with one of them being com.docke. I terminated the other two processes by using command: sudo kill [pid], restarted the machine, and Voilà, I was finally able to connect to the instance successfully!

--

--