This example uses ...
-
Django
as the “glue” between web browsers and a database1 -
Postgres
with extensionTimescaleDB
as the web application database2 -
Celery
as a task queue powered byRedis
Tip
If you have any trouble getting setup, feel free to ask a question at django-timescaledb-example/discussions
-
Install
Postgres
8 and thusTimescaleDB
8
Important
For Windows
try tporadowski/redis
or zkteco-home/redis-windows
. Celery
is used alongside Redis
as a task queue & may not run in which case you'll have to adapt **/tasks.py
for Bogdanp/dramatiq
-
Install this project's
Python
dependencies via ...poetry install
-
Create a
.env
file from.env.dist
to store credentials ...cp .env.dist .env
Warning
Create a complex secret key10 if you intend to deploy this web application
-
Create a
TimescaleDB
database with userdjango
via thePostgres
CLI ...sh shell/createdb.sh
Important
For Windows
adapt shell/createdb.sh
for Command Prompt
or PowerShell
Warning
Create a new role with a password if you go on to do something with this database
-
Create a superuser to access the
admin
site ...poetry run python manage.py createsuperuser
-
Launch
Django
,Redis
&Celery
...honcho start
Tip
Go to http://localhost:8000
& you should see a running web application
- Re-launch the database server ...
pg_ctl start -D .db/
- Launch a shell within your virtual environment so you don't need
poetry run
** to runDjango
commands likepoetry run python manage.py ...
...
poetry shell
Footnotes
Footnotes
-
To display a web page it asks a database for the data it needs to render files that the browser interprets (HTML, CSS & JavaScript) so it can display a user interface ↩
-
TimescaleDB
is an extension to thePostgres
database which grants it timeseries capabilities.Postgres
wasn't designed to handle timeseries workloads in which data is infrequently inserted and frequently queried in bulk.TimescaleDB
adaptsPostgres
via "hypertables" which enable compression of many rows into "chunks" which are indexed by timestamps. Consequently, queries on ranges of timestamps are faster sincePostgres
can search "chunks" instead of rows & storage is cheaper. By compressing,TimescaleDB
trades insert performance for query performance. ↩ -
I use
git clone
...git clone git@github.com:rdmolony/rdmolony.github.io.git
... since I prefer to authenticate with
GitHub
viaSSH
↩ -
I use
nix
...
↩nix profile install nixpkgs#python3
-
I use
nix
...nix profile install nixpkgs#pipx
Why not use
pip
?Python
ships withpip
which installs dependencies "globally" which means that you can't easily install the same 3rd party library twice ↩ -
I use
pipx
...
↩pipx install poetry
-
I use
pipx
...
↩pipx install honcho
-
I use
nix
...
↩ ↩2nix profile install --impure --expr 'with import <nixpkgs> {}; pkgs.postgresql.withPackages (p: [ p.timescaledb ])'
-
I use
nix
...
↩nix profile install nixpkgs#redis
-
Generate a
SECRET_KEY
...poetry run python -c " from django.core.management.utils import get_random_secret_key print(get_random_secret_key()) "
... & copy it into
.env
↩