简体   繁体   中英

Docker postgres change username and password

I am using the mdillon/postgis image as my containerized database. I have created a database that is owned by a user I created using the image's username and password environment variables. Now I need to changes that user's username and password.

I successfully changed the password but I could not change the username because you can change the current user's username.

I tired logging in as the postgres user but I get an error saying theres is no such user.

Any ideas?

Connect to the database as the firstly created user (lets call that user old_user ) and create a new superuser

CREATE USER mysuperuser WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'mysuperuser'

Now disconnect and connect again as that suepruser. Then change old_user user's password:

ALTER USER "old_user" WITH PASSWORD 'new_password';

Then change old_user user's username:

ALTER USER old_user RENAME TO "new-user";

Finally reconnect as the new-user and delete the superuser

DROP USER mysuperuser;

You can see all users with:

SELECT usename
FROM pg_catalog.pg_user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM