PostgreSQL with PostGIS on Android


Having PostgreSQL including PostGIS up and running on your Android device

Published on February 13, 2023 by Stefanie Janine Stölting

PostgreSQL postgres Android Termux F-Droid PostGIS

3 min READ

Using PostgreSQL On Android

There is an app for Android users, which will give you a terminal: Termux.

With it’s package management you can install your beloved PostgreSQL. That does also include having PostGIS.

There might also be an old Termux app available in the Google Play Store. Do not install this version, it is outdated and will not be updated anymore!

Installation Of F-Droid

F-Droid is an alternative package app store for Android.

To install it, you need to change your Android settings and to allow external installations.

After the installation is done, you open the F-Droid and search for the Termux package.

Installation Of PostgreSQL

After the start of Termux, update all already installed packages with the package manager pkg.

pkg upgrade

pgk will ask you to install the available updates, the default answer is Y, you can just hit the enter key.

The next step is already to install PostgreSQL packages with pgk.

pkg install postgresql

PostgreSQL Initialization

Now you need to initialize PostgreSQL, the package does not do this.

-- Create a database directory
mkdir -p $PREFIX/var/lib/postgresql

-- Run initdb to initialize PostgreSQL
initdb $PREFIX/var/lib/postgresql

Start And Stop PostgreSQL

PostgreSQL will not be started automatically, you need to set it up on your own, or just start and stop it manually.

-- Start PostgreSQL
pg_ctl -D $PREFIX/var/lib/postgresql start

-- Stop PostgreSQL before you leave Termux!
pg_ctl -D $PREFIX/var/lib/postgresql stop

PostgreSQL Usage

We need to open psql to work with PostgreSQL.

-- Open the default database with the local user
psql postgres
-- Create your own role, use your own name here, please ;-)
CREATE ROLE stefanie WITH superuser login password '';

-- Create a database owned by your own user
CREATE DATABASE stefanie WITH OWNER stefanie;

-- List all databases
\l
   Name    |  Owner   | Encoding | Collate |  Ctype  | ICU Locale | Locale Provider |  Access privileges
-----------+----------+----------+---------+---------+------------+-----------------+---------------------
 postgres  | u0_a382  | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            |
 stefanie  | stefanie | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            |
 template0 | u0_a382  | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            |
 template1 | u0_a382  | UTF8     | C.UTF-8 | C.UTF-8 |            | libc            |
(4 rows)
-- Leave psql
CTRL d

From now on you can use your own user name in your own database.

psql stefanie

Installation Of PostGIS

Since a while now, PostGIS is available as a package on Termux, too.

pkg install postgis

PostGIS Usage

To use PostGIS you have to open psql. (Please use your own user name ;-)

psql -U stefanie
CREATE EXTENSION postgis;
CREATE EXTENSION

Now you can use PostGIS on your Android mobile, how cool is that?



Author: Stefanie Janine Stölting