Skip to content

postgresml/postgresml

Repository files navigation

Postgres ML demo

Quick demo with Postgres, PL/Python, and Scikit.

Installation in WSL or Ubuntu

Install Python3, pip, and Pl/Python3:

sudo apt update
sudo apt install -y postgresql-plpython3-12 python3 python3-pip

Restart the Postgres server:

sudo service postgresql restart

Create the extension:

CREATE EXTENSION plpython3u;

Install Scikit globally (I didn't bother setup Postgres with a virtualenv, but it's possible):

sudo pip3 install sklearn

Run the demo

sudo mkdir /app/models
sudo chown postgres:postgres /app/models
psql -f scikit_train_and_predict.sql

Example output:

psql:scikit_train_and_predict.sql:4: NOTICE:  drop cascades to view scikit_train_view
DROP TABLE
CREATE TABLE
psql:scikit_train_and_predict.sql:14: NOTICE:  view "scikit_train_view" does not exist, skipping
DROP VIEW
CREATE VIEW
INSERT 0 500
CREATE FUNCTION
 scikit_learn_train_example
----------------------------
 OK
(1 row)

CREATE FUNCTION
 value | weight | prediction
-------+--------+------------
     1 |      5 |          5
     2 |      5 |          5
     3 |      5 |          5
     4 |      5 |          5
     5 |      5 |          5
(5 rows)

Run the linear model

Install our PgML package globally:

cd pgml
sudo python3 setup.py install
cd ../

Run the test:

psql -f sql/test.sql

Make sure to run it exactly like this, from the root directory of the repo.