Can not connect Postgresql to DOcker-compose

445 Views Asked by At

Using docker-compose I'm trying to create a database, I thought perhaps I have another superuser for that error, but when checking

postgres=# \du

                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

The docker-compose.yml file:

version: '3.8'

services:

 # Backend API
    smart-brain-api:
        container_name: backend
        build: ./
        command: npm start
        working_dir: /usr/src/smart-brain-api
        # environment:
        #     POSTGRES_USER: test1
        #     POSTGRES_PASSWORD: test2
        #     POSTGRES_DB: smart-brain-docker
        #     POSTGRES_HOST: postgres
            # POSTGRES_HOST_AUTH_METHOD: trust
        # links:
        #     - postgres
        ports:
            - "3001:3001"
        volumes:
            - ./:/usr/src/smart-brain-api

 # Postgres
    postgres:
        # environment:
            # POSTGRES_USER: test1
            # POSTGRES_PASSWORD: test2
            # POSTGRES_DB: smart-brain-docker
            # POSTGRES_HOST: postgres
            # POSTGRES_HOST_AUTH_METHOD: trust
        image: postgres
        ports:
            - "5432:5432"

The commented out stuff is what I use further in the file, and additionally by me: POSTGRES_HOST_AUTH_METHOD: trust the suggested stuff - no success.

I also tried changing POSTGRES_HOST: to postgres1 - in case it interfere with my superuser postgres - no success.

I even tried changing POSTGRES_USER and POSTGRES_PASSWORD to my actual superuser username and password.

And this is my Dockerfile

FROM node:alpine

WORKDIR /usr/src/smart-brain-api

COPY ./ ./

RUN npm install

CMD ["/bin/bash"]

The error after docker-compose up --build

Successfully tagged smart-brain-api_smart-brain-api:latest
Creating postgres ... done
Creating backend ... done                                                                                         
Attaching to backend, postgres
postgres           | Error: Database is uninitialized and superuser password is not specified.
postgres           |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres           |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres           |
postgres           |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres           |        connections without a password. This is *not* recommended.
postgres           |
postgres           |        See PostgreSQL documentation about "trust":
postgres           |        https://www.postgresql.org/docs/current/auth-trust.html
postgres exited with code 1
backend            |

I also noticed when I ignored this error, and went further I would only get CREATE DATABASE by postgres_1

While in the course he is getting:

...
CREATE DATABASE
CREATE ROLE
...

Also I use -L for nodemon in package.json (not sure if that helps in finding the issue, I only HAD to use it as it wouldn't read my new changes).

When I simply type psql it tries to let me login with username of my windows (which is not set as postgres user) so I must use psql -U postgres to login, is that "default" user causing some problems to the superuser?

I will keep trying differen and report back.

0

There are 0 best solutions below