Home

Awesome

pg - PostgreSQL connector for Tarantool

Build and Test

Getting Started

Prerequisites

Installation

Clone repository and then build it using CMake:

git clone https://github.com/tarantool/pg.git
cd pg && cmake . -DCMAKE_BUILD_TYPE=RelWithDebugInfo
make
make install

You can also use LuaRocks:

luarocks install https://raw.githubusercontent.com/tarantool/pg/master/pg-scm-1.rockspec --local

See tarantool/rocks for LuaRocks configuration details.

Usage

local pg = require('pg')
local conn = pg.connect({host = localhost, user = 'user', pass = 'pass', db = 'db'})
local tuples = conn:execute("SELECT $1 AS a, 'xx' AS b", 42)
conn:begin()
conn:execute("INSERT INTO test VALUES(1, 2, 3)")
conn:commit()

API Documentation

conn = pg:connect(opts = {})

Connect to a database.

Options:

Returns:

conn:execute(statement, ...)

Execute a statement with arguments in the current transaction.

Returns:

Example:

tarantool> conn:execute("SELECT $1 AS a, 'xx' AS b", 42)
---
- - - a: 42
      b: xx
    ...

conn:begin()

Begin a transaction.

Returns: true

conn:commit()

Commit current transaction.

Returns: true

conn:rollback()

Rollback current transaction.

Returns: true

conn:ping()

Execute a dummy statement to check that connection is alive.

Returns:

pool = pg.pool_create(opts = {})

Create a connection pool with count of size established connections.

Options:

Returns

conn = pool:get()

Get a connection from pool. Reset connection before returning it. If connection is broken then it will be reestablished. If there is no free connections then calling fiber will sleep until another fiber returns some connection to pool.

Returns:

pool:put(conn)

Return a connection to connection pool.

Options

Comments

All calls to connections api will be serialized, so it should to be safe to use one connection from some count of fibers. But you should understand, that you can have some unwanted behavior across db calls, for example if another fiber 'injects' some sql between two your calls.

See Also