Skip to content

Instantly share code, notes, and snippets.

@jonathlt
Last active December 6, 2022 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathlt/3ec9b72f868298edfee2f90b17384fd4 to your computer and use it in GitHub Desktop.
Save jonathlt/3ec9b72f868298edfee2f90b17384fd4 to your computer and use it in GitHub Desktop.
Install Postgres 15 with Postgis from source on Raspberry Pi 4

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.

Build and install postgres

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

Build and Install postgis

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

Post install steps

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment