4

I have created a Postgresql docker image using the following command on Windows:

docker run --name airdb-postgres -e POSTGRES_PASSWORD=post1234 -d -p 5432:5432 postgres:alpine

is it possible to change the password I assigned to it, or I should create a new one by disposing this image?

brainoverflow
  • 691
  • 2
  • 9
  • 22
  • 1
    Name of the environment variable is `PGPASSWORD` not `POSTGRES_PASSWORD` (of course provided that there is already a user with this password). However can you please elaborate on what exactly you are trying to do? – mic4ael Oct 31 '19 at 13:02
  • I wasn't sure how it is possible to change the password of a postgresql docker image. I realized I can do it after logging into the container @mic4ael . – brainoverflow Oct 31 '19 at 14:30

2 Answers2

18

You should be able to do that by logging into the container

docker exec -it <container_id> bash

then use psql cli to change the password.

See How to change PostgreSQL user password? for the latter part.

Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
msrc
  • 663
  • 1
  • 9
  • 19
  • 1
    I get: > psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist – NessDan Feb 03 '23 at 10:28
  • @NessDan what command gave you that error? – msrc Feb 15 '23 at 15:36
  • @NessDan By default, it will log you in as a the user you are using if you do not have any arguments specified. Which, if you connect to the database using the `docker exec` command, will be `root`. You can specify the database name and the username within the `psql` command. Example: `psql mydatabase myuser`. See `psql --help` for more information about the command. – bleuthoot Mar 01 '23 at 15:14
2

When I was upgrading postgres 12 to 15 for some reason my user passwords weren't working after the backup and restore. I was able to change fix it by doing the following. On windows I used git bash to run these commands.

bash

cat reset_password.sql | docker exec -i your-db-container-id psql -U postgres

reset_password.sql

ALTER USER postgres WITH PASSWORD 'mysecretpassword';

ALTER USER oc_admin WITH PASSWORD 'mysecretpassword';

lastlink
  • 1,505
  • 2
  • 19
  • 29