FROM rockylinux/rockylinux:8

# Install packages
RUN dnf install -y sudo make gcc llvm-toolset openssl-devel redhat-rpm-config

# Install and enable EPEL repository
RUN dnf -y install epel-release
RUN crb enable

# 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 dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-`uname -m`/pgdg-redhat-repo-latest.noarch.rpm

# Disable PostgreSQL in default repository
RUN dnf -qy module disable postgresql

# Enable PG18 repo (can be removed after the official release)
RUN dnf config-manager --set-enabled pgdg18-updates-testing

# Install PostgreSQL
ENV PGVERSION=18

RUN dnf install -y postgresql${PGVERSION?}-server postgresql${PGVERSION?}-devel postgresql${PGVERSION?}-contrib

# Configure llvm lib (required for aarch64)
RUN echo /opt/rh/llvm-toolset-7.0/root/usr/lib64/ > /etc/ld.so.conf.d/llvm-toolset.conf
RUN ldconfig

# Create PostgreSQL cluster
ENV PGBIN=/usr/pgsql-${PGVERSION}/bin
ENV PGDATA="/var/lib/pgsql/${PGVERSION}/data"
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
