Not sure which postgres image are you using.

If you look at the official postgres image for complete information. It allows user to specify the environment variables for the below ones and these can be easily overridden at run time.

  • POSTGRES_PASSWORD
  • POSTGRES_USER
  • PGDATA
  • POSTGRES_DB
  • POSTGRES_INITDB_ARGS

Environment variables can be overridden using below three methods depending on your case.

  • Run Image: If running docker image directly, use below to include environment variable in docker run with -e K=V. Please refer documentation for more details here
docker run -e POSTGRES_PASSWORD=secrect -e POSTGRES_USER=postgres <other options> image/name
  • Dockerfile: If you need to specify the environment variable in Dockerfile, specify as mentioned below. Please refer documentation for more details here
ENV POSTGRES_PASSWORD=secrect
ENV POSTGRES_USER=postgres
  • docker-compose: If you need to specify the environment variable in docker-compose.yml, specify as below. Please refer documentation for more details here
web:
  environment:
    - POSTGRES_PASSWORD=secrect
    - POSTGRES_USER=postgres

Hope this is useful.

Answer from Rao on Stack Overflow
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
The main caveat to note is that postgres doesn't care what UID it runs as (as long as the owner of PGDATA matches), but initdb does care (and needs the user to exist in /etc/passwd): $ docker run -it --rm --user www-data -e POSTGRES_PASSWORD=mysecretpassword postgres The files belonging to ...
Discussions

How to change the postgresql password within docker
Are you using the official postgres container? There are environment variables you can pass in to control what the username and password are. Look for "Environment Variables" here: https://hub.docker.com/_/postgres More on reddit.com
🌐 r/docker
4
2
April 16, 2024
How to pass default user and password (postgres) to docker start
I followed the documentation but I can not connect via the postgres protocol because I don't know the default credentials. The documentations seems not to cover how I can set user/password for ... More on github.com
🌐 github.com
1
1
postgresql - Docker: you must specify POSTGRES_PASSWORD - Stack Overflow
I'm trying to dockerize my backend API (Ruby on Rails and Postgres), I'm stuck in the postgres setting up, as a first step I would like to show you the docker log: Basically, I think the most impo... More on stackoverflow.com
🌐 stackoverflow.com
How to create postgres superuser role and DB on docker-compose.yml
Did u even Google this? Sheesh https://stackoverflow.com/questions/56249917/how-to-give-a-postgres-user-superuser-previllege-through-docker-compose More on reddit.com
🌐 r/docker
13
4
November 23, 2022
🌐
Docker Community
forums.docker.com › general
How create username postgresql container - General - Docker Community Forums
October 22, 2022 - Example during creation I ran the command: “” docker run --name postgresql -e POSTGRES_USER = myusername -e POSTGRES_PASSWORD = mypassword -p 5432: 5432 -v / data: / var / lib / postgresql / data -d postgres " "With this command I created ...
🌐
Warp
warp.dev › terminus by warp › docker › how to launch a postgresql container in docker
How To Launch A PostgreSQL Container In Docker - Warp
January 31, 2024 - This command launches a Postgres container with a new root user named admin, identified by the password admin. By default, Postgres creates a database matching the name of the user defined in the POSTGRES\_USER variable.
🌐
Reddit
reddit.com › r/docker › how to change the postgresql password within docker
r/docker on Reddit: How to change the postgresql password within docker
April 16, 2024 -

Hello, I recently got into working within docker and postgresql and I was wondering how I can change the login info for postgresql within docker as I don't have the postgresql files within my PC and can't change it through the pg_hba_conf file method and couldn't find out how to do after looking into the problem and was curious if anyone on this sub had a clue on how to solve it?

🌐
Medium
medium.com › @beld_pro › quick-tip-creating-a-postgresql-container-with-default-user-and-password-8bb2adb82342
Quick Tip: Creating a PostgreSQL Container with default User and Password | by beld 🎩 | Medium
April 30, 2017 - . ├── Dockerfile └── init └── 01-filladb.shcat ./DockerfileFROM postgres:alpine ADD ./init /docker-entrypoint-initdb.d/
🌐
GitHub
github.com › frodenas › docker-postgresql › blob › master › README.md
docker-postgresql/README.md at master · frodenas/docker-postgresql
September 27, 2020 - POSTGRES_EXTENSIONS to create extensions for the above database (only takes effect is a database is specified) On this example we will preset our custom username and password and we will create a database with a extension: $ docker run -d \ --name postgresql \ -p 5432:5432 \ -e POSTGRES_USERNAME=myuser \ -e POSTGRES_PASSWORD=mypassword \ -e POSTGRES_DBNAME=mydb \ -e POSTGRES_EXTENSIONS=citext \ frodenas/postgresql
Author   frodenas
Find elsewhere
🌐
SquaredUp
squaredup.com › blog › running-postgres-in-docker
Running Postgres in Docker - SquaredUp
May 21, 2025 - In the Connection tab enter "localhost" as the Host name/address and then under Password enter the password you set when you created your Postgres container ("admin" in the example I provided): Click Save and it will automatically connect to ...
🌐
OneUptime
oneuptime.com › home › blog › how to run postgresql in docker and docker compose
How to Run PostgreSQL in Docker and Docker Compose
January 21, 2026 - docker run --name postgres \ -e POSTGRES_PASSWORD=mysecretpassword \ -v $(pwd)/postgres-data:/var/lib/postgresql/data \ -p 5432:5432 \ -d postgres:16 · PostgreSQL Docker image supports several environment variables: ... services: postgres: ...
🌐
Towards Data Science
towardsdatascience.com › home › latest › getting started with postgres in docker
Getting started with Postgres in Docker | Towards Data Science
March 5, 2025 - We can "inject" all variables from this file into the Docker container upon running it (more information in this article). Let’s first create our environment file that’ll contain our database credentials. We’ll call the file dbcredentials.env and add the following content: POSTGRES_HOST=my_db POSTGRES_USER=mike POSTGRES_PASSWORD=supersecretpassword POSTGRES_DB=my_project_db
🌐
DataCamp
datacamp.com › tutorial › postgresql-docker
PostgreSQL in Docker: A Step-by-Step Guide for Beginners | DataCamp
April 14, 2025 - docker run --name postgres-db \ -e POSTGRES_PASSWORD=mypassword \ -e POSTGRES_USER=myuser \ -e POSTGRES_DB=mydatabase \ -p 5432:5432 \ -v postgres-data:/var/lib/postgresql/data \ -d postgres
🌐
BlueVPS
bluevps.com › blog › how to create user in postgresql? (postgresql create user)
How to Create User in Postgresql - BlueVPS.com
May 5, 2025 - docker run --name postgres_db -e POSTGRES_PASSWORD=mysecretpassword -d postgres
🌐
OneUptime
oneuptime.com › home › blog › how to set up postgresql with docker
How to Set Up PostgreSQL with Docker
February 2, 2026 - # Using a bind mount instead of a named volume docker run --name my-postgres \ -e POSTGRES_USER=myuser \ -e POSTGRES_PASSWORD=mypassword \ -v /path/to/your/data:/var/lib/postgresql/data \ -p 5432:5432 \ -d postgres:16
🌐
Medium
medium.com › @yahyaali.se › setting-up-postgres-with-docker-desktop-9e4c2e77cd7c
Setting up Postgres with Docker Desktop 🐘 | by Yahya Ali | Medium
February 28, 2024 - There are some default values that setup if you are not careful and you end up debugging the errors for very long time. This is for local setup purposes only. ... Verify the image is in using docker desktop. It should look something like this ... docker run --name <container_name> -e POSTGRES_PASSWORD=<password> -e POSTGRES_USER=<root> -p 5432:5432 -d postgres
🌐
Code4IT
code4it.dev › blog › run-postgresql-with-docker
How to run PostgreSQL locally with Docker | Code4IT
November 25, 2024 - Of course, we’re defining the username and password of the admin user, as well as the name of the database. -d indicates that the container run in a detached mode. This means that the container runs in a background process. postgres is the name of the image we are using to create the container. As a result, you will see the newly created container on the CLI (running docker ...
🌐
GitHub
gist.github.com › narate › f1077566f7f267b5b1911bcbe530cea6
Create PostgreSQL user/password wirh database in PostgreSQL running in docker container · GitHub
docker-compose exec -T \ -e DB_USER=new_user \ -e DB_PASSWORD=userpwd \ -e DB_NAME=new_db \ pg_database sh 2>/dev/null < create-pg-user.sh
🌐
SQL Shack
sqlshack.com › getting-started-with-postgresql-on-docker
Getting started with PostgreSQL on Docker
August 12, 2022 - docker run –name pgsql-dev -e POSTGRES_PASSWORD=Welcome4$ -p 5432:5432 Postgres
🌐
Hevo
hevodata.com › home › learn › database management system
How to Install Docker PostgreSQL Container? [5 Easy Steps]
March 24, 2026 - PostgreSQL is the name of the Docker Container. -e POSTGRES_USER is the parameter that sets a unique username to the Postgres database. -e POSTGRES_PASSWORD is the parameter that allows you to set the password of the Postgres database.