this post was submitted on 26 Mar 2024
12 points (100.0% liked)

Docker

1076 readers
2 users here now

founded 1 year ago
MODERATORS
 

I'm trying to create a postgres container, I have the following in my Docker Compose:

db:
  container_name: db
  image: postgres
  restart: always
  environment:
    #POSTGRES_USER="postgres"
    POSTGRES_PASSWORD: HDFnWzVZ5bGI
  ports:
    - 5432:5432
  volumes:
    - pgdata:/var/lib/postgresql/data
adminer:
  container_name: adminer
  image: adminer
  restart: always
  ports:
    - 8338:8080

And yet Docker keeps saying that the database is initialized and that the superuser is not specified. Where am I going wrong?

I've tried with and without equals, a hyphen, quotation marks. No matter what I try, it won't see it.

#Solution:

Find:

  volumes:
    - pgdata:/var/lib/postgresql/data

Replace:

  volumes:
    - /opt/postgres/data:/var/lib/postgresql/data

More info: https://lazysoci.al/comment/8597610

top 13 comments
sorted by: hot top controversial new old
[–] [email protected] 4 points 4 months ago (1 children)

Try removung the # from infront of POSTGRES_USER, this designates it as a comment.

[–] [email protected] 1 points 4 months ago

Thanks, I tried with and without the POSTGRES_USER line commented out, still not joy. The documentation says it should default to default when not declared.

[–] [email protected] 4 points 4 months ago* (last edited 4 months ago) (2 children)

If there is already an initialized database, the environment variables will be ignored.

What you have looks correct; the POSTGRES_USER variable only needs to be set if you want it to be something other than postgres.

Note: If you use dashes, I believe it's KEY=Value and if you don't use dashes, it's KEY: value

Try giving it a clean data volume and re-running to see if it picks it up.

[–] [email protected] 2 points 4 months ago

Thank you so much, your comment set me in the right direction. Turns out the tutorial I followed which had the data volume empty was the culprit*. So when I actually mapped it to something, it started working.

  • I'm blaming the tutorial, but it was totally my fault!
[–] [email protected] 2 points 4 months ago

Thanks, I'll give this a go during breakfast. I've been pulling out my hair and this seems the most likely solution I didn't think of.

[–] [email protected] 3 points 4 months ago (1 children)

As others have said, remove the # to uncommit the line.

Commits are a special type of line in many languages that allow us humans to stick info (generally for humans) inside the code that the interpreter skips over. From the machines perspective this block looks like:

environment:
    POSTGRES_PASSWORD: HDFnWzVZ5bGI

Note that the entire line is missing.

As a side note. Please change the password as it's been posted to the Internet.

[–] [email protected] 1 points 4 months ago (1 children)

Thanks, I tried with and without the POSTGRES_USER line commented out, still not joy. The documentation says it should default to default when not declared.

As for the password, don't worry I changed it right away.

[–] [email protected] 2 points 4 months ago (1 children)

I assume there is nothing in the database? Delete the file under volumes and relaunch. At a guess your database for initialized without a user and is now just in that state.

[–] [email protected] 1 points 4 months ago (1 children)

Turns out I didn't actually have a volume, so nothing was actually created properly.

[–] [email protected] 2 points 4 months ago

Yep. Sounds right. Welcome to learning docker compose.

[–] [email protected] 3 points 4 months ago (1 children)

It's commented out... Remove the #

[–] [email protected] 1 points 4 months ago

Thanks, I tried with and without the POSTGRES_USER line commented out, still not joy. The documentation says it should default to default when not declared.

[–] [email protected] 3 points 4 months ago

Just took a quick look at my config file and I have

POSTGRES_USER: postgres

Note specifically the colon and lack of quotes.

It's also worth noting I'm using postgres:16-bullseye as my image. Something wasn't working right with latest when I was setting it up a few months ago, but I don't think it was the user. Regardless, worth a shot if the previous change doesn't help.