Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A basic Postgres driver for Lua

branch: master
README.md

Lua Postgres

A Postgres library for Lua. Other than the basic connection and query execution functionality you would expect in any database driver, it also supports automatic query parameter escaping via libpq, meta information about tables and result sets. Support for asynchronous queries is planned but not yet implemented.

I'm doing this in part because I need it, and in part to learn more about C programming. My total experience with C is reading K&R, and about 3 weeks of practice. Use this library at your own risk! That said, my need for this library grew out of frustrations with memory leaks, bugs and lack of maintainence with other existing Lua Postgres libraries, and so I've been fairly diligent about testing and keeping the code small and simple to read. Any bugs will likely be easy for an experienced C programmer to spot.

Why?

LuaSQL is OK, but the fact that it's an abstraction library means that by design, it will not support some of Postgres's useful vendor-specific features, like asynchronous queries, array and hash types, etc.

Other Postgres drivers I found for Lua were woefully buggy, incomplete, and abandoned, so I've decided to work on this one.

My goal is to create a solid, general-purpose but Postgres-specific library that exposes the most useful features of Postgres in a clean, Lua-idiomatic way rather than just a direct translation of libpq to Lua.

Compatibility

  • Lua 5.1 and 5.2
  • Verified working on Linux and OS X. Should work fine on *BSD but not yet tested.
  • Windows (probably, but not tested. Please let me know if you have problems.)

Example code

local postgres = require "postgres"
local conn, err = postgres.connection("dbname: my_database")
if err then error(err) end

local function emit(query)
  local result, err = conn:execute(query)
  if not result then error(err) end
  return result
end

emit "DROP TABLE IF EXISTS people"
emit "CREATE TABLE people(id SERIAL NOT NULL PRIMARY KEY, name VARCHAR(255))"
emit "insert into people (name) values ('Joe Schmoe')"
emit "insert into people (name) values ('John Doe')"
emit "insert into people (name) values ('Jim Beam')"

print(conn:execute("SELECT * FROM people WHERE id IN ($1)", "1,2,3"))

Acknowledgements

Some ideas and bits of code in this library are taken from Lua-Pgsql and Luapgsql, as well as LuaSQL.

Where to get it

http://github.com/norman/lua-postgres

License

MIT/X11

Something went wrong with that request. Please try again.