top

Package pgstore is a PostgreSQL-based storage engine for the SCS session package.

A working PostgreSQL database is required, containing a sessions table with the definition:

CREATE TABLE sessions (
  token TEXT PRIMARY KEY,
  data BYTEA NOT NULL,
  expiry TIMESTAMPTZ NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions (expiry);

The pgstore package provides a background 'cleanup' goroutine to delete expired session data. This stops the database table from holding on to invalid sessions indefinitely and growing unnecessarily large.

Usage:

func main() {
    // Establish a database/sql pool
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // Create a new PGStore instance using the existing database/sql pool,
    // with a cleanup interval of 5 minutes.
    engine := pgstore.New(db, 5*time.Minute)

    sessionManager := session.Manage(engine)
    http.ListenAndServe(":4000", sessionManager(http.DefaultServeMux))
}

The pgstore package is underpinned by the pq driver (https://github.com/lib/pq).

Imported only in test by 1 package(s)

  1. github.com/alexedwards/scs

Imports 1 package(s)

  1. github.com/lib/pq

Test imports 1 package(s)

  1. github.com/alexedwards/scs/session