gogs / gogs

Gogs is a painless self-hosted Git service
https://gogs.io
MIT License
45.06k stars 4.86k forks source link

Add PostgreSQL support for session provider #4234

Open hochhaus opened 7 years ago

hochhaus commented 7 years ago

The session provider currently supports memory, file, redis and mysql. This issue is a feature request to add postgres support as well.

Postgres is supported upstream. Therefore, I suspect this feature addition should be a relatively minor change.

KostyaEsmukov commented 4 years ago

I've managed to set up postgres as backend for sessions.

Here's what I've had to do:

  1. Apply the following patch on the current master and rebuild gogs:
diff --git a/internal/conf/conf.go b/internal/conf/conf.go
index 9ba14f38..ddef312f 100644
--- a/internal/conf/conf.go
+++ b/internal/conf/conf.go
@@ -17,6 +17,7 @@ import (
    _ "github.com/go-macaron/cache/memcache"
    _ "github.com/go-macaron/cache/redis"
    _ "github.com/go-macaron/session/redis"
+   _ "github.com/go-macaron/session/postgres"
    "github.com/gogs/go-libravatar"
    "github.com/mcuadros/go-version"
    "github.com/pkg/errors"
-- 
2.25.0

Without that I've been getting error: panic: session: unknown provider 'postgres'(forgotten import?)

  1. Create the sessions table (see https://go-macaron.com/middlewares/session#postgresql), I used the same database as gogs:
CREATE TABLE session (
    key       CHAR(16) NOT NULL,
    data      BYTEA,
    expiry    INTEGER NOT NULL,
    PRIMARY KEY (key)
);
  1. Specify postgres as sessions provider in app.ini:
[session]
PROVIDER = postgres
PROVIDER_CONFIG = user=gogs password=p@ssw0rd host=mypostgres.local port=5432 dbname=gogs sslmode=verify-full