Setting up Geodjango on Arch Linux

Tags: linux django arch database postgis geodjango postgres

Installing Postgres

sudo pacman -S postgresql

Setting up data directory within my /home partition:

mkdir -p /home/myuser/postgresdb_data/data
sudo chown -R postgres:postgres /home/myuser/postgresdb_data

Initializing data:

sudo -iu postgres
initdb -D /home/myuser/postgresdb_data/data

Edit the systemd service file:

sudo systemctl edit postgresql

Add the following:

[Service]
Environment=PGROOT=/home/myuser/postgresdb_data
PIDFile=/home/myuser/postgresdb_data/data/postmaster.pid
ProtectHome=false

Edit /home/myuser/postgresdb_data/data/postgresql.conf to listen exclusively through UNIX sockets and we'll also set password encryption to scram-sha-256:

...
listen_addresses = ''
...
password_encryption = scram-sha-256
...

Edit /home/myuser/postgresdb_data/data/pg_hba.conf to restrict access rights to the database superuser and configure access for our django user:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             postgres				peer
local	geodjango	db_user				scram-sha-256

Installing PostGIS

sudo pacman -S postgis

Create the spatial database:

sudo -iu postgres
createdb geodjango
psql geodjango
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_tiger_geocoder;

Create The Database User

CREATE USER db_user WITH PASSWORD 'secret-password';
ALTER ROLE db_user SET client_encoding TO 'utf8';
ALTER ROLE db_user SET default_transaction_isolation TO 'read committed';
ALTER ROLE db_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE geodjango TO db_user;

Errors

At this point if I try to interact with the database, I get an error about a missing pgrouting file so apparently I'm still missing that part.

ERROR:  could not open extension control file "/usr/share/postgresql/extension/pgrouting.control": No such file or directory

Installing pgRouting

git clone git://github.com/pgRouting/pgrouting.git
cd pgrouting
git checkout v3.1.3
mkdir build
cd build
cmake ..

Ah shit, another error:

CMake Error at /usr/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least
  version "1.53.0")
Call Stack (most recent call first):
  /usr/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.20/Modules/FindBoost.cmake:2345 (find_package_handle_standard_args)
  CMakeLists.txt:76 (find_package)

Install boost

sudo pacman -S boost

Back to installing pgRouting:

cd pgrouting/build
cmake ..
make
sudo make install

Ok sweet, now to create the extension:

sudo -iu postgres
psql geodjango
CREATE EXTENSION pgrouting;

Done!