POSTGRES_USER not taken into account

Viewed 243

I'm trying to deploy postgres as a StatefulSet on k3d.

I set the environment variable POSTGRES_USER, but when I try to connect to the db, it isn't taken into account and I see that authentication has failed. I can login with the password pgpassword and default user postgres.

Why isn't the POSTGRES_USER taken into account?

This is my yml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql-db
spec:
  serviceName: postgresql-db-service
  selector:
    matchLabels:
      app: postgresql-db
  replicas: 2
  template:
    metadata:
      labels:
        app: postgresql-db
    spec:
      containers:
        - name: postgresql-db
          image: postgres:latest
          volumeMounts:
            - name: postgresql-db-disk
              mountPath: /data
          env:
            - name: POSTGRES_USER
              value: pguser
            - name: POSTGRES_PASSWORD
              value: pgpassword
            - name: PGDATA
              value: /data/pgdata
      volumes:
        - name: postgresql-db-disk
          hostPath:
            # directory location on host
            path: /opt/x/projects/tilt_test/data/
            # this field is optional
            type: DirectoryOrCreate
1 Answers

but when i try to connect to the db, it isn't taken into account, and i see that authentication has failed.

It works exactly as it should. The POSTGRES_USER and POSTGRES_PASSWORD system variables are used to create the default user and password when creating the database. If, on the other hand, you intend to log in to an existing user later, you must set and use system variables: PGUSER and PGPASSWORD. Here is the explanation:

  • PGUSER behaves the same as the user connection parameter.

  • PGPASSWORD behaves the same as the password connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using a password file (see Section 33.15).

You can also find a complete tutorial How to Deploy PostgreSQL Statefulset in Kubernetes With High Availability. Additionally, you will find an explanation for POSTGRES_USER and POSTGRES_PASSWORD system variables:

POSTGRES_USER: The user that should be created automatically when the Postgres process starts. POSTGRES_PASSWORD: The password for the user created by default.

In this case, logging into the database takes place during readiness and liveness probes.

Related