BackendPyPgSQL() Class Introduction
This class implements the auth.Sessions.Backend virtual base class, extending it to store the session information in a PostgreSQL database using the PyPgSQL database driver. See the documentation for auth.Sessions.Backend() for more information on the methods and other details of it's use.
The instantiation of this class takes a PyPgSQL database connect string. This string is of the form:
'host:port:database:user:password:options:tty'
Leading components can be empty, trailing components can be omitted entirely. For example, to specify a database name of "testdb" while leaving all other parameters as the default, you would use "::testdb".
BackendPyPgSQL() Database Schema
This backend expects to have the databases "users" and "sessions" set up as follows. The tables may have other fields in them, but any additional columns must have defaults specified in the schema.
CREATE TABLE users ( id SERIAL, username TEXT UNIQUE NOT NULL, cryptpasswd TEXT NOT NULL, createddate INTEGER NOT NULL, ts_created TIMESTAMP DEFAULT NOW(), lastlogin INTEGER DEFAULT NULL, lasthit INTEGER DEFAULT NULL, payload TEXT NOT NULL ); -- Web-site session information CREATE TABLE sessions ( id SERIAL, key TEXT UNIQUE NOT NULL, username TEXT NOT NULL, cryptpasswd TEXT NOT NULL, createddate INTEGER NOT NULL, ts_created TIMESTAMP DEFAULT NOW(), expiresecs INTEGER DEFAULT NULL, expires INTEGER DEFAULT NULL, payload TEXT NOT NULL );
Note that you may also have to grant privileges on these tables to the user that the web-server is running under. For example, if your server runs as the user Apache:
GRANT ALL ON users TO apache; GRANT ALL ON users_id_seq TO apache; GRANT ALL ON sessions TO apache; GRANT ALL ON sessions_id_seq TO apache;
Requirements
This backend relies on having the PyPgSQL database driver installed. PyPgSQL is not a part of JOTWeb, it is a stand-alone Python DB-API2 driver. For more information or to download PyPgSQL, see the PyPgSQL homepage.
Example
See the "auth.Sessions.Backend()" page, which includes an example of using the "BackendFilesystem()" class. This class can be used in the same way, except it would be instantiated with "BackendPyPgSQL('::authdb')" if the name of your authentication database is "authdb", for example.
Also, in the source code see "examples/test009/testbe-pypgsql", which is an extensive test suite for the "BackendPyPgSQL()" class.