PostgresStorageNode

From OpenCog

The PostgresStorageNode is a StorageNode that can save/restore Atoms to a PostgreSQL database. It uses exactly the same API as all the other StorageNodes; see StorageNode for details.

The PostgresStorageNode is a multi-user node. That is, multiple AtomSpaces can all open up the same Postgres database at the same time, stuff atoms into it, read them out, delete them, and so on. Because Postgres is distributed and scalable, one could have, in principle, thousands of AtomSpaces doing this all at the same time. In practice, no one has ever tried doing this with more than three AtomSpaces at once (to the best of my knowledge. World record holders are invited to update this wiki page.) It is, of course, up to you to make sure that all these instances are not stepping on one-another.

Example

Here's a very short example that opens a connection to a Postgres database, and fetches all of the Values attached to a given Atom.

(define psn (PostgresStorageNode "postgres://opencog_tester:cheese@127.0.0.1/opencog_test"))
(cog-open psn)
(fetch-atom (Concept "foo") psn)
(cog-close psn)

Complete, functional demos are provided here: examples/atomspace/persistence.scm and examples/atomspace/persist-multi.scm and examples/atomspace/persist-query.scm.

URL format

The URL encodes the username opencog_tester, the password cheese, the name of the database opencog_test and the hostname localhost. That is, URL has the generic format

postgres://username:password@hostname/dbname

The username, password and hostname are optional; if not specified, defaults will be obtained from the environment, according to the conventional Postgres configuration rules. Thus, depending on the configuration, the following URL's should work as well:

postgres://username@hostname/dbname
postgres://hostname/dbname
postgres:///dbname

The last form assumes localhost aka 127.0.0.1.

Earlier versions also supported odbc:// URL's, but this has been removed because ODBC is a real pain-in-the-neck to configure, and is 2x slower than the native Postgres API.

Configuration

Setting up and configuring Postgres for use with the AtomSpace is rather complex. See the Configuring PostgreSQL wiki page for how to do this. Most users will be happier with RocksStorageNode, which does not require any configuration; it just writes to the local filesystem. it is also roughly 4x or 8x faster than PostrgresStorageNode.

See also