0

I am attempting to install the dockerized version of mastodon and although I've referred to several guides (including official documentation), I'm consistently getting an error connecting to the postgres database.

Here the relevant section of my docker-compose.yml:

  db:                                                                                                                                                
     restart: always                                                                                                                                  
     image: postgres:9.6-alpine                                                                                                                       
     shm_size: 256mb                                                                                                                                  
     networks:                                                                                                                                        
       - internal_network                                                                                                                             
     healthcheck:                                                                                                                                     
       test: ["CMD", "pg_isready", "-U", "postgres"]                                                                                                  
     volumes:                                                                                                                                         
       - ./postgres:/var/lib/postgresql/data 

... and my .env.production:

# PostgreSQL                                                                                                                                         
 # ----------                                                                                                                                         
 DB_HOST=/var/run/postgresql                                                                                                                          
 DB_USER=mastodon                                                                                                                                     
 DB_NAME=mastodon_production                                                                                                                          
 DB_PASS=[MY PASSWORD]   <--- (I've also tried leaving this blank with no difference in the result)                                                                                                                           
 DB_PORT=5432

But when I run (for example): $ docker-compose run --rm web rails db:migrate

I get the following output:

    rails aborted!
    PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

and $ docker logs mastodon_db_1 gives me:

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":
       https://www.postgresql.org/docs/current/auth-trust.html

I'm relatively new to docker but (I think) I understand the basic concepts. However, a lot of Googling has me thinking that I'm the only one who's having this issue and I can't see what I'm missing.

1
  • I was able to fix this by renaming .env.production to .env because docker-compose reads variables from .env file
    – Rajat Jain
    May 29, 2021 at 20:22

1 Answer 1

2

The second error is because of missing ENV that is required for Postgres container.

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser

POSTGRES_PASSWORD

This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable

You need to specify POSTGRES_PASSWORD ENV

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: example
      POSTGRES_DB: mastodon_production
      POSTGRES_USER: mastodon

postgres-docker-Environment Variables

While the second error seems the host is invalid or you not consuming properly from the Environment variable.

PG::ConnectionBad: could not connect to server: No such file or directory

change the dot env file to

 DB_HOST=db                                                                                                                          
 DB_USER=mastodon                                                                                                                                     
 DB_NAME=mastodon_production                                                                                                                          
 DB_PASS=example  <--- (the one which is set in DB)                                                                                                                           
 DB_PORT=5432

DB_HOST should be a service name to connect from another container to DB.

1
  • Yes, as usual all I had to do was give up and post a question on SE to start making headway. Right after I posted this I tried adding the environment variable to docker-compse.yml, and also updated the postgres host to mastodon_db_1 (I had to make a similar update to the redis host). I had tried those in various combinations before, but obviously not the magic (i.e. correct) way. Jul 28, 2020 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.