pg-el
This Emacs Lisp library lets you access the PostgreSQL đ object-relational DBMS from Emacs, using its network-level frontend/backend protocol. The library is capable of automatic type coercions from a range of SQL types to and from the equivalent Emacs Lisp type.
This libary will be useful for developers, rather than end users. If youâre looking for an Emacs-based browser/editor for PostgreSQL, you may be interested in PGmacs, which uses this library to communicate with PostgreSQL or a compatible database.
-
SCRAM-SHA-256 authentication (the default method since PostgreSQL version 14) as well as MD5 and password authentication.
-
Encrypted (TLS) connections between Emacs and the PostgreSQL backend. This includes support for client certificates.
-
Prepared statements using PostgreSQLâs extended query message flow, that allows for parameterized queries to protect from SQL injection issues.
-
The PostgreSQL COPY protocol to copy preformatted data to PostgreSQL from an Emacs buffer.
-
Asynchronous handling of LISTEN/NOTIFY notification messages from PostgreSQL, allowing the implementation of publish-subscribe type architectures (PostgreSQL as an âevent brokerâ or âmessage busâ and Emacs as event publisher and consumer).
-
Parsing various PostgreSQL types including integers, floats, array types, numerical ranges, JSON and JSONB objects into their native Emacs Lisp equivalents. The parsing support is user-extensible. Support for the HSTORE, pgvector and PostGIS extensions.
-
Connections over TCP or (on Unix machines) a local Unix socket.
The code has been tested with PostgreSQL versions 17beta2, 16.3, 15.4, 13.8, 11.17, and 10.22 on Linux. It is also tested via GitHub actions on MacOS and Microsoft Windows. This library also works, to a variable extent, against other databases that implement the PostgreSQL wire protocol:
-
YugabyteDB: tested against version 2.21. This database uses a lot of code from PostgreSQL 11 and is quite compatible, including with the HSTORE and pgvector extensions. However, some system tables differ from PostgreSQL, such as the
pg_sequences
table. It does not support the XML type. It does not supportLISTEN
/NOTIFY
. -
CrateDB: tested with version 5.7.2. There are limitations in this databaseâs emulation of the PostgreSQL system tables: for example, itâs not possible to query the owner of a table (function
pg-table-owner
). It doesnât accept SQL statements that only include an SQL comment. It doesnât support setting comments on SQL tables. As documented, CrateDB does not support theTIME
type without a time zone. It doesnât support casting integers to bits. It doesnât support theVARBIT
type. It has no support for the COPY protocol. -
CockroachDB: tested with CockroachDB CCL v24.1. Note that this database does not implement the large object functionality, and its interpretation of SQL occasionally differs from that of PostgreSQL. It is currently reporting an internal error when we call
pg-table-comment
. -
ParadeDB: This ElasticSearch alternative is very PostgreSQL-compatible (more of an extension than a reimplementation). Tested with the Dockerhub instance which is based on PostgreSQL 16.3. All tests pass.
-
QuestDB: tested against version 6.5.4. This is not very PostgreSQL-compatible: it fails on the SQL query
SELECT 1::integer
because it doesnât recognize integer as a type. It doesnât supportDELETE
statements. -
ClickHouse doesnât work with pg-el. Their version 24.5 has a very basic implementation of the PostgreSQL wire protocol. It doesnât support the
pg_type
system table which provides information on the OIDs associated with different PostgreSQL types. All values are returned in textual format using the pseudo-OID of 0, which means the client must parse the value. The database immediately closes the connection on any SQL error. It doesnât support configuration statements such asSET datestyle
. It doesnât specify aserver_name
in the startup sequence, which might allow us to detect this special case and restrict functionality to the most basic aspects.
Tested with Emacs versions 30-pre-release, 29.4, 28.2, 27.2 and 26.3. Emacs versions older than
26.1 will not work against a recent PostgreSQL version (whose default configuration requires
SCRAM-SHA-256 authentication), because they donât include the GnuTLS support which we use to
calculate HMACs. They may however work against a database set up to allow unauthenticated local
connections. Emacs versions before 28.1 will not support the extended query protocol, because the
bindat
package is required. We mostly test with Emacs on Linux, but the library also works fine on
Microsoft Windows and MacOS.
You may be interested in an alternative library emacs-libpq that enables access to PostgreSQL from Emacs by binding to the libpq library.
Licence
pg-el is free software distributed under the terms of the GNU GPL v3 or later.