title: Docker Postgres psql connection: FATAL: role "..." does not exist
body: <p>I am using docker-compose to build a node express &amp; Postgres application.</p>
  <p><strong>Problem</strong><br />
  I cannot enter the container with psql to inspect the tables but my initial migration is logging that there was a successful migration.</p>
  <p>--- EDIT ---
  <a href="https://github.com/MrT3313/ReactQuery_Demo" rel="nofollow noreferrer">Codebase</a>
  --- END EDIT ---</p>
  <p><strong>Running Containers</strong></p>
  <p>note: the db is immediately shutting down and this might be the reason my psql is failing while the migration (which is creating a new connection) is working??</p>
  <pre><code>## -- LOG FROM RUNNING DB CONTAINER -- ##
  
  2021-12-04 22:08:27.683 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
  
  2021-12-04 22:08:27.683 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
  
  2021-12-04 22:08:27.688 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
  
  2021-12-04 22:08:27.723 UTC [76] LOG:  database system was shut down at 2021-12-04 22:08:27 UTC
  
  2021-12-04 22:08:27.734 UTC [1] LOG:  database system is ready to accept connections
  </code></pre>
  <p><a href="https://i.stack.imgur.com/vGWrx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vGWrx.png" alt="enter image description here" /></a></p>
  <p><strong>✅ Successful Migration Command</strong><br />
  <code>docker exec reactquery_demo-backend-1 npm run migrate</code></p>
  <p><strong>❌ Failed psql Command(s)</strong></p>
  <ol>
  <li>trying to connect to db w/ docker user: <code>psql -p 5432 -d reactquery_demo_db -U docker</code></li>
  <li>trying to connect to db container w/ docker user:<code>psql -p 5432 -d reactquery_demo-db-1 -U docker</code></li>
  </ol>
  <p>THE ERROR:<br />
  <code>psql: error: connection to server on socket &quot;/tmp/.s.PGSQL.5432&quot; failed: FATAL:  role &quot;docker&quot; does not exist</code></p>
  <ul>
  <li>I have seen posts about this being caused by the fact that a POSTGRES_USER is not added in the docker-compose but I have that in mine (as shown below)</li>
  <li>I have also tried:
  <ul>
  <li>uninstalling / reinstalling / upgrading postgresql w/ homebrew</li>
  <li>running <code>rm /usr/local/var/postgres/postmaster.pid</code> and then restarting Postgres w/ homebrew</li>
  <li>stopping the homebrew Postgres processes completely so that is does not conflict with docker Postgres??
  <ul>
  <li>when I do this I get the following error: <code>psql: error: connection to server on socket &quot;/tmp/.s.PGSQL.5432&quot; failed: No such file or directory Is the server running locally and accepting connections on that socket?</code></li>
  </ul>
  </li>
  </ul>
  </li>
  </ul>
  <hr />
  <p><strong>Repo Structure</strong></p>
  <p><a href="https://i.stack.imgur.com/zoxQq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zoxQq.png" alt="enter image description here" /></a></p>
  <p><strong>Package.json</strong></p>
  <pre><code>{
    &quot;name&quot;: &quot;ReactQueryDemo&quot;,
    &quot;version&quot;: &quot;0.0.1&quot;,
    &quot;description&quot;: &quot;Simple Overview of React Query for Weekly Dev Meeting&quot;,
    &quot;main&quot;: &quot;index.js&quot;,
    &quot;scripts&quot;: {
      &quot;dev&quot;: &quot;node index.js&quot;,
      &quot;dev:watch&quot;: &quot;nodemon index.js&quot;,
      &quot;migrate&quot;: &quot;node db/scripts/migrate.js&quot;
    },
    &quot;dependencies&quot;: {
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;knex&quot;: &quot;^0.95.14&quot;,
      &quot;pg&quot;: &quot;^8.7.1&quot;
    }
  }
  </code></pre>
  <p><strong>Docker Compose</strong></p>
  <pre><code>version: '3.8'
  
  services:
    backend: 
      build: ./backend
      ports: 
        - '8080:8080'
      depends_on:
        - db
  
    db:
      image: 'postgres'
      ports: 
        - '5432:5432'
      environment:
        - POSTGRES_PASSWORD=password
        - POSTGRES_USER=docker
        - POSTGRES_DB=react_query_demo_db
      volumes: 
        - data:/var/lib/postgresql/data
  
  volumes:
    data:
  </code></pre>
  <p><strong>Dockerfile</strong></p>
  <pre><code>FROM node:17
  
  WORKDIR /backend
  
  # copy everything from local backend dir
  # into your current working directory in
  # the container: /backend
  COPY . .
  
  # install dependencies in the container
  # the .dockerignore does not copy the 
  # node_modules || Dockerfile itself
  RUN npm i
  
  CMD [&quot;npm&quot;, &quot;run&quot;, &quot;dev&quot;]
  </code></pre>
  <p><strong>Backend Index</strong></p>
  <pre><code>// server
  const server = require(&quot;./api/server.js&quot;);
  
  // port
  const PORT = process.env.PORT || 8080;
  
  // listening
  server.listen(PORT, () =&gt; {
    console.log(`*** \n\tServer listening on http://0.0.0.0:${PORT}\n***`)
  });
  </code></pre>
  <p><strong>Server</strong></p>
  <pre><code>// import packages
  const express = require('express')
  
  // import routes
  
  // create server
  const server = express()
  // configure server
  server.use(express.json())
  
  // configure routing
  
  // base route
  server.get('/', async(req,res) =&gt; {
    console.log('React Query Demo Server Running')
    res.json({ status: &quot;SUCCESS&quot;, message: &quot;React Query Demo Server Running&quot; })
  })
  
  // exports
  module.exports = server
  </code></pre>
  <p><strong>Database Index</strong></p>
  <pre><code>// imports
  const knex = require('knex')
  
  module.exports = knex({
    client: 'postgres',
    connection: {
      host: 'db',             // this is the name of the service in the docker-compose
      user: 'docker',
      password: 'password',
      database: 'react_query_demo_db'     
    }
  })
  </code></pre>