0

I messed up a bit with a Dockerized PostgreSQL instance and accidentally overwrote the values I had for POSTGRES_USER and POSTGRES_PASSWORD. They were randomly generated, so guessing them is basically out of the question. By nature of Dockerized PostgreSQL, if you specify your own username, the role postgres will not be created. That means that postgres, admin, root are all invalid roles on my DB instance.

Now I'm stuck with a data directory in a volume that I can access and a psql that I cannot access because I don't know any valid username on that database. Is there a list of usernames in this data directory that I can somehow access to get inside and change the password?

illright
  • 111
  • 2

2 Answers2

1

Okay, I found a way. According to PostgreSQL docs on the structure of the data directory:

Item Description
global Subdirectory containing cluster-wide tables, such as pg_database

So I connected to the container running the DB with docker exec -it <container-name> /bin/sh and went to /var/lib/postgresql/data/global. There were a bunch of binary files, and I started printing their contents one-by-one until I found a string that I recognized. It was found in file 1260, though I doubt that's going to be the case all the time.

Then I simply connected to the database (the Docker version of the database is configured in such a way that you don't need a password to connect from within) using psql -U <recovered-username> and changed the password with \password '<recovered-username>'. Success!

illright
  • 111
  • 2
-1

Saving it in clear text anywhere, would defeat the point of the passwords !!!

Your only option is to Save what you can and reinstall. This time use GIT or something to keep successive backups of important files.

Rohit Gupta
  • 2,116
  • 8
  • 19
  • 25