- postgres-as-a-cache
Use postgres as a cache
.\start-pg.ps1-
Creates a custom variation of posgres from
Dockerfile, Adds in plugins we want (see file above), Configures plugins -
Starts container running, which starts base postgres
-
Reconfigures postgres + Restarts postgres, via:
./data/configure_pg.sh- Finishes up by running SQL script:
./data/pg_cron_add.sh- Postgres w. plugins ready for use
Notes:
- Horrible work arounds, if you have a better way, create an issue, or put in a PR
- It works though.
postgresql://postgres:password123-@localhost:5432/postgres
.\bash-pg.ps1It opens a bash shell...with handy guidance
SQL Scripts Folder: /var/lib/postgresql/data
Postgres Logs: /var/log/postgresql
Postgres Utilities Folder: /usr/lib/postgresql/16/bin
In general to run postgres commands you will have to run as the 'postgres' user
su -- postgres -c {pg_command}
root@9022c51945a7:/var/lib/postgresql/data#
.\stop-pg.ps1- Stops image
- Does a tear down (you can customize to stop this, change start too.
I got the idea by reading this fantastic article by info@dizzy.zone you can find it https://dizzy.zone/2025/09/24/Redis-is-fast-Ill-cache-in-Postgres/
Create a handy cache with few simple methods, see Data Schema
Make sure you have: Powershell 7+
Checklist
Prefer: the snake_case convention for postgres names, avoiding reserved words
- Decide on the DB to host your cache
- Decide on a unique schema name to host your cache artifacts, thus deleting the schema deletes the cache
- Decide if you need a custom role, and if so, pick a unique name
.\make-cache.ps1 `
-ConnectionString "postgresql://postgres:password123-@localhost:5432/postgres" `
-SchemaName "test_cache_01" `
-RoleName "test-cache-role"Arguments:
ConnectionString: valid Postgres Connection String (sample is the docker one)SchemaName: (required) schema to put the cache intoRoleName: (unused, future)
- Takes the schema in
sql\that start with##_, starting at the minimum index of 110 inclusive - In each file replaces
{schema}token with your schema name, and{rolename}with your role name (not used for now) - Copies transformed files into
temp\folder which is emptied first - Execute scripts in numeric order ascending at the postgres instance and database in the connection string
- When done, the cache is ready for use
This will create the objects in Data Schema
Copied from pg_cron
┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31) or last day of the month ($)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to
│ │ │ │ │ Saturday, or use names; 7 is also Sunday)
│ │ │ │ │
│ │ │ │ │
* * * * *
An easy way to create a cron schedule is: crontab.guru.
See: Cron Setup