A simple, minimal PostgreSQL session store for Connect/Express
A simple, minimal PostgreSQL session store for Express/Connect
npm install connect-pg-simple
Once npm installed the module, you need to create the session table in your database. For that you can use the [table.sql] (https://github.com/voxpelli/node-connect-pg-simple/blob/master/table.sql) file provided with the module:
psql mydatabase < node_modules/connect-pg-simple/table.sql
Or simply play the file via a GUI, like the pgAdminIII queries tool.
Examples are based on Express 4.
var session = ;app;
Advanced example showing some custom options:
var pg =session =pgSession = session;app;
Express 3 (and similar for Connect):
var express = ;app;
DATABASE_URLenvironment variable (as you do by default on eg. Heroku) – then you need to specify the connection string or object here so that this module that create new connections. Needen even if you supply your own database module.
session, then you can specify that here.
60seconds. If set to
falseno automatic pruning will happen. Automatic pruning weill happen
pruneSessionIntervalseconds after the last pruning – manual or automatic.
console.error(), but can be useful to override if one eg. uses Bunyan for logging.
3.0.0, then the timers will block any graceful shutdown unless you tell the automatic pruning to stop by closing the session handler using this method.
false– which can be useful if one wants improved control of the pruning.
store.touch()method to allow for extending the life time of a session without changing the data of it. This enables setting the
false, which is recommended to avoid a session extender save overwriting another save that adds new data to the session. More info in the express-session readme.
pginstance used is created by this module, then this module should also close it on
close()method – or by deactivating the automatic pruning by settinging
false. If automatic pruning is disabled the client needs to call
pruneSessions()manually or otherwise ensure that old sessions are pruned.
publicschema, as added in
2.2.0, but rather default to the pre-
2.2.0behavior of no defined schema. This to ensure backwards compatibility with the
2.xbranch, per semantic versioning best practise.
require('pg')to match package.json, thanks for reporting @dmitriiabramov
pgdependency again rather than
pg.jsas the latter will be discontinued as
pgnow fills its role
express.sessionto the module rather than just