Skip to content

Commit a7aa7b7

Browse files
authored
Merge pull request #1017 from rmartinsen/997-reconfigure-docker-setup
Add timescale and pg_stat_statement postgres extensions
2 parents f1f261b + 22c7b8e commit a7aa7b7

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

data/Dockerfile-timescale

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM postgres:16-bullseye
2+
3+
ENV POSTGIS_MAJOR=3
4+
ENV POSTGIS_VERSION=3.5.0+dfsg-1.pgdg110+1
5+
ENV TIMESCALE_MAJOR=2
6+
ENV TIMESCALE_MINOR=17
7+
8+
RUN apt-get update \
9+
&& apt-get install -y --no-install-recommends lsb-release curl gnupg apt-transport-https wget \
10+
# ca-certificates: for accessing remote raster files;
11+
# fix: https://github.com/postgis/docker-postgis/issues/307
12+
ca-certificates \
13+
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \
14+
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts \
15+
# Add timescale repository and key
16+
&& echo "deb https://packagecloud.io/timescale/timescaledb/debian/ $(lsb_release -c -s) main" \
17+
| tee /etc/apt/sources.list.d/timescaledb.list \
18+
&& wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey \
19+
| gpg --dearmor -o /etc/apt/trusted.gpg.d/timescaledb.gpg \
20+
# Install timescaledb
21+
&& apt-get update \
22+
&& apt-get install -y --no-install-recommends timescaledb-$TIMESCALE_MAJOR-postgresql-$PG_MAJOR="$TIMESCALE_MAJOR.$TIMESCALE_MINOR*" postgresql-client-$PG_MAJOR \
23+
# Remove temporary files
24+
&& rm -rf /var/lib/apt/lists/* \
25+
&& rm -f /etc/apt/trusted.gpg.d/timescaledb.gpg
26+
27+
28+
RUN mkdir -p /docker-entrypoint-initdb.d
29+
30+
# Set up TimescaleDB extension during database initialization
31+
RUN echo "shared_preload_libraries='timescaledb'" >> /usr/share/postgresql/postgresql.conf.sample

data/docker-compose.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ services:
6262
context: .
6363
dockerfile: Dockerfile-pg
6464
environment:
65-
- PGPORT=5433
66-
- POSTGRES_PASSWORD
65+
PGPORT: 5433
66+
POSTGRES_PASSWORD:
6767
restart: always
6868
ports:
6969
- '5433:5433'
@@ -75,5 +75,24 @@ services:
7575
extra_hosts:
7676
- host.docker.internal:host-gateway
7777

78+
postgres-timescale:
79+
container_name: cagp-postgres-timescale
80+
build:
81+
context: .
82+
dockerfile: Dockerfile-timescale
83+
environment:
84+
PGPORT: 5434
85+
POSTGRES_PASSWORD:
86+
restart: always
87+
ports:
88+
- '5434:5434'
89+
volumes:
90+
- timescale_database_volume:/var/lib/postgresql/data
91+
- ./init_pg_timescale.sql:/docker-entrypoint-initdb.d/init_pg.sql
92+
- /etc/timezone:/etc/timezone:ro
93+
- /etc/localtime:/etc/localtime:ro
94+
extra_hosts:
95+
- host.docker.internal:host-gateway
7896
volumes:
7997
database_volume:
98+
timescale_database_volume:

data/init_pg_timescale.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE DATABASE vacantlotdb;
2+
\c vacantlotdb;
3+
CREATE EXTENSION postgis;
4+
CREATE EXTENSION pg_stat_statements;
5+
CREATE EXTENSION timescaledb;

docs/SETUP/BACK_END.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ To stop the postgres container run:
118118
docker compose down postgres
119119
```
120120

121+
### PostgreSQL Extensions
122+
123+
We use Postgres extensions for GIS and time series functionality not included in base Postgres.
124+
125+
#### PostGIS
126+
[PostGIS](https://postgis.net/) is an open-source extension for PostgreSQL that adds support for spatial and geographic data types and functions. It enables the storage, querying, and analysis of location-based data directly within the database, replacing the need for many external tools and libraries.
127+
128+
#### Timescale DB
129+
[TimescaleDB](https://docs.timescale.com/) is an open-source relational database built on PostgreSQL, optimized for handling time-series data efficiently.
130+
131+
At the core of TimescaleDB are hypertables, which partition data across time for efficient querying. Hypertables behave like normal Postgres tables, but are optimized for querying data based on timestamps. For our use case, hypertables simplify data management by automatically creating monthly partitions, replacing our previous method of manually creating a separate schema for each month.
132+
133+
#### pg_stat_statements
134+
135+
The [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) extension provides detailed statistics on query performance, helping to identify slow or resource-intensive queries. It tracks execution counts, execution times, and rows returned, making it a useful tool for analyzing slow or problematic queries.
136+
121137
## Python Development
122138

123139
You can set up your local Python environment so you can develop and run the backend `script.py` and create and run unit tests outside of Docker. Build your local environment to match what is defined in the `Dockerfile`. Install the same python version as is in the Dockerfile, using `pyenv` to manage multiple distributions if needed. Use `pipenv` to create a virtual environment. Install the pip dependencies that are defined in the `Pipfile` into your virtual environment. Install the executables with `apt-get`. Now you can develop in Python in your terminal and IDE and run unit tests with `pytest`.

0 commit comments

Comments
 (0)