FROM ubuntu:noble

# Install packages
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y sudo wget gnupg tzdata locales lsb-release apt-utils make gcc libssl-dev \
    libkrb5-dev

# Remove the default ubuntu user to reduce the chance of a conflict with the host user
RUN userdel ubuntu

# Create postgres user/group with specific IDs
ARG UID=1000
ARG GID=1000

RUN groupadd -g $GID -o postgres
RUN useradd -m -u $UID -g $GID -o -s /bin/bash postgres

# Add PostgreSQL repository
RUN apt-get install -y --no-install-recommends postgresql-common
RUN /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y -c 18

# Install PostgreSQL
ENV PGVERSION=18

RUN apt-get install -y postgresql-${PGVERSION?} postgresql-server-dev-${PGVERSION?}

# Create PostgreSQL cluster
ENV PGBIN=/usr/lib/postgresql/${PGVERSION}/bin
ENV PGDATA="/var/lib/postgresql/${PGVERSION}/test"
ENV PATH="${PATH}:${PGBIN}"

RUN sudo -u postgres ${PGBIN?}/initdb -A trust -k ${PGDATA?}
RUN echo "shared_preload_libraries = 'pgaudit'" >> ${PGDATA}/postgresql.conf

# Configure sudo
RUN echo 'postgres ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers

USER postgres
