bitnami / containers

Bitnami container images
https://bitnami.com
Other
3.26k stars 4.75k forks source link

[bitnami/postgrest] Postgrest container not connecting to DB with correctly set DB_* env variables #71678

Closed Kerruba closed 1 week ago

Kerruba commented 1 week ago

Name and Version

bitnami/potgrest:latest (11.2.2)

What architecture are you using?

amd64

What steps will reproduce the bug?

  1. I'm deploying the latest potgrest image in Kubernetes using this manifest for testing purposes:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: postgrest
    namespace: databases
    spec:
    replicas: 1
    selector:
    matchLabels:
      app: postgrest
    template:
    metadata:
      labels:
        app: postgrest
    spec:
      containers:
        - name: postgrest
          image: bitnami/postgrest:latest
          ports:
            - containerPort: 3000
          env:
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: postgres
            - name: DB_HOST
              value: my-postgres.databases.svc.cluster.local
            - name: DB_NAME
              value: mydb
            - name: DB_PASSWORD
              value: *****

    Postgres is up and running and exposed via a service in the same namespace as PostgREST, and I'm able to access it with regular psql within k8s and also from my local machine by portforwarding the port 5432.

When the Pod starts, it tells me is not able to connect to the database:

{"code":"PGRST000","details":"connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: No such file or directory\n\tIs the server running locally and accepting connections on that socket?\n","hint":null,"message":"Database connection error. Retrying the connection."}
  1. I attached it to the container and I couldn't find a configuration file for PostgREST, so I created one using the example configuration found on their site and started postgrest with that configuration, and it worked.

  2. Checking the README, I've tried to manually add the PGRST_DB_URI env variable to the deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: postgrest
    namespace: databases
    spec:
    replicas: 1
    selector:
    matchLabels:
      app: postgrest
    template:
    metadata:
      labels:
        app: postgrest
    spec:
      containers:
        - name: postgrest
          image: bitnami/postgrest:latest
          ports:
            - containerPort: 3000
          env:
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: postgres
            - name: DB_HOST
              value: my-postgres.databases.svc.cluster.local
            - name: DB_NAME
              value: mydb
            - name: DB_PASSWORD
              value: *****
            - name: PGRST_DB_URI
              value: postgresql://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)

    The container is now starting correctly and the application can connect to the database.

What is the expected behavior?

By using the env variables listed in the README.md as customizable I would be able to connect to the database correctly

What do you see instead?

I get the connection error

{"code":"PGRST000","details":"connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: No such file or directory\n\tIs the server running locally and accepting connections on that socket?\n","hint":null,"message":"Database connection error. Retrying the connection."}

and the only way of fixing this is by using the PGRST_DB_URI which I thought was read-only and not supposed to be changed

Kerruba commented 1 week ago

Another issue I'm facing is the default anonymous user. From the README (and dockerhub page), seems like anon is by default setup as the anonymous user, but if not configured manually with the correct env-variable is not able to access the apis, even if the user is correctly setup in the database

            - name: PGRST_DB_ANON_ROLE
              value: "anon"
javsalgar commented 1 week ago

Hi!

In the case of postgrest, we are not adding any container logic, so it is just the plain binary. In this sense, these issues you are experimenting are of the application itself, so my advice is to check with the upstream devs.

https://github.com/PostgREST/postgrest

Kerruba commented 1 week ago

Hi @javsalgar, thanks for your reply.

The confusion is coming from the documentation on the bitnami docker image. In the configuration section are mentioned many env-variables and their default value, but I couldn´t find any reference on the PostgREST website in configuration documentation. Specifically, the DB_* with their defaults are not mentioned anywhere. There's though a reference to LIBPQ env variables .

Also some defaults are not what described in the PostgREST documentation, e.g. the PGRST_DB_ANON_ROLE env-variable: https://postgrest.org/en/stable/references/configuration.html#db-anon-role)

Maybe is just a matter of updating the README to point to POSTGREST documentation?

javsalgar commented 1 week ago

Hi!

I understand the confusion. These env variables are used in the VM of Supabase, but not in the container or helm chart, so it safe to ignore them and just check the variables in the upstream documentation. I will let the team know that we should update the README.md (the env var section is auto-generated)

Kerruba commented 1 week ago

Ok thank you very much for the clarification. I'll leave the issue open, feel free to close it when appropriate :+1: