Postgres/Postgis packages on linux are not often the latest versions. Installing from source is not too difficult. This is also an exercise to see if it's possible to run the database on a pi. Compiling may take a while on more lower spec machines.
Download source
https://ftp.postgresql.org/pub/source/v15.1/postgresql-15.1.tar.gz
Unzip
tar -xvf postgresql-15.1.tar.gz
Move to source dir
cd postgresql-15.1
Install libreadline-dev
sudo apt-get install libreadline-dev
Configure, make and install everything except documentation
./configure
make world-bin
sudo make install
Create the postgres user if it doesn't exist
adduser postgres
Create the data dir and initialise the database
sudo mkdir -p /usr/local/pgsql/data
sudo chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
Install dependencies
sudo apt-get install libxml2-dev libgeos-dev libproj-dev libprotobuf-c-dev protobuf-c-compiler libgdal-dev
Download and unzip
wget https://download.osgeo.org/postgis/source/postgis-3.3.2.tar.gz
tar xvzf postgis-3.3.2.tar.gz
cd postgis-3.3.2
Configure, make and install
./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config
make
sudo make install
In /usr/local/pgsql/data/postgresql.conf uncomment listen_addresses = ‘*’ open pg_hba.conf and add following entry at the very end.
host all all 0.0.0.0/0 md5
host all all ::/0 md5
sudo nano /usr/lib/systemd/system/postgresql.service
fill the file with this script
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=simple
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
RuntimeDirectory=postgresql
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload