Skip to content
pg_redis
Esc
navigateopen⌘Jpreview
On this page

Quickstart

Install pg_redis with Docker or as Postgres extensions, then talk Redis on :6379.

pg_redis makes PostgreSQL speak Redis. Pick an install path, then point any Redis client at the RESP port — the gateway runs inside Postgres.

Install

Fastest path: a Postgres image with pg_cron, pg_redis, and the in-process RESP gateway already wired.

docker run --rm -d \
  --name pg_redis \
  -p 6379:6379 \
  -p 5432:5432 \
  -e POSTGRES_USER=redis \
  -e POSTGRES_PASSWORD=redis \
  -e POSTGRES_DB=pg_redis \
  asamsig/pg_redis:16
Host port Inside the container What
6379 6379 Redis protocol (RESP)
5432 5432 PostgreSQL

Map whatever host ports you want — for example -p 5762:6379 if 6379 is taken, then use redis-cli -p 5762.

Stop / remove:

docker stop pg_redis

Install into an existing PostgreSQL 16 cluster when you already run Postgres and only want the Redis-facing bits.

1. Dependencies

pg_redis needs pg_cron for active TTL expiry (lazy expiry still works without it, but expired keys won’t be swept in the background).

sudo apt-get update
sudo apt-get install -y postgresql-16-cron

Install pg_cron for your Postgres major so CREATE EXTENSION pg_cron works, then continue below.

2. Install the pg_redis packages

Grab the release artifacts for your Postgres major (control / SQL / .so) from GitHub Releases and install them into the cluster’s extension dirs:

# Example layout after unpacking a pg16 package
PG_SHARE="$(pg_config --sharedir)"
PG_LIB="$(pg_config --pkglibdir)"

sudo cp -a extension/. "$PG_SHARE/extension/"
sudo cp -a lib/.       "$PG_LIB/"

You need both:

Extension Role
pg_redis Storage API — schema redis
pg_redis_gw_host In-process RESP BGWorker

3. Configure Postgres

Add to postgresql.conf (or use ALTER SYSTEM), then restart the server:

shared_preload_libraries = 'pg_cron,pg_redis_gw_host'

# pg_cron must target the database where you’ll create the extensions
cron.database_name = 'pg_redis'

# Gateway host
redis_gateway.database = 'pg_redis'
redis_gateway.setup_configuration_file = '/etc/pg_redis/SetupConfiguration.json'

Drop a config file at that path (see the repo example):

{
  "GatewayListenPort": 6379,
  "UseLocalHost": false,
  "PostgresHostName": "127.0.0.1",
  "PostgresPort": 5432,
  "PostgresDatabase": "pg_redis",
  "PostgresSystemUser": "redis",
  "PostgresPassword": "redis",
  "RequirePostgres": true,
  "StorageMode": "unlogged"
}

GatewayListenPort is the RESP port clients use (default 6379). Tune storage with StorageMode — see Storage modes. Full env/JSON map: Configuration.

4. Create the extensions

-- Connect to the database named in cron.database_name
CREATE EXTENSION pg_cron;
CREATE EXTENSION pg_redis;
CREATE EXTENSION pg_redis_gw_host;

After create, the BGWorker listens on GatewayListenPort.

Talk Redis

With the gateway up, use any Redis client against the RESP port:

redis-cli -p 6379 PING
# PONG

redis-cli -p 6379 SET session:1 '{"user":"ada"}'
redis-cli -p 6379 GET session:1
# {"user":"ada"}

redis-cli -p 6379 EXPIRE session:1 60
redis-cli -p 6379 TTL session:1

If you published RESP on another host port (for example -p 5762:6379):

redis-cli -p 5762 PING

Point your app at it

Same URL shape as Redis:

REDIS_URL=redis://127.0.0.1:6379

Works with ioredis, redis-py, go-redis, StackExchange.Redis, and anything else that speaks RESP. See Commands for the current surface.

Peek at SQL storage

The same keys live in schema redis (Postgres reserves pg_* schema names):

# Docker install
docker exec -it pg_redis \
  psql -U redis -d pg_redis -c "SELECT * FROM redis.strings;"

# Extension install (local psql)
psql -U redis -d pg_redis -c "SELECT * FROM redis.strings;"

Values are bytea (shown hex-encoded in psql).

Next steps

Was this page helpful?