Shard Detail

pg v0.9.0

a postgres driver for crystal

Install & Use

Add the following code to your project's shard.yml under:

dependencies to use in production
- OR -
development_dependencies to use in development

  github: will/crystal-pg
  version: 0.9.0



A Postgres driver for Crystal Build Status



Add this to your shard.yml and run crystal deps

    github: will/crystal-pg


require "pg"
DB = PG.connect("postgres://...")

typed querying

The preferred way to send queries is to send a tuple of the types you expect back along with the query. #rows will then be an array of tuples with each element properly casted. You can also use parameterized queries for injection-safe server-side interpolation.

result = DB.exec({Int32, String}, "select id, email from users")
result.fields  #=> [PG::Result::Field, PG::Result::Field]
result.rows    #=> [{1, ""}], …]
result.to_hash #=> [{"field1" => value, …}, …]

result = DB.exec({String}, "select $1::text || ' ' || $2::text", ["hello", "world"])
result.rows #=> [{"hello world"}]

Out of the box, crystal-pg supports 1-32 types. If you need more, you can reopen PG::Result and use the generate_gather_rows macro. If your field can return nil, you should use Int32|Nil for example, which is a union of the type and Nil.

untyped querying

If you do not know the types beforehand you can omit them. However you will get back an array of arrays of PGValue. Since it is a union type of amost every type, you will probably have to manually cast later on in your program.

result = DB.exec("select * from table")
result.fields  #=> [PG::Result::Field, …]
result.rows    #=> [[value, …], …]
result.to_hash #=> [{"field1" => value, …}, …]

result = DB.exec("select $1::text || ' ' || $2::text", ["hello", "world"])
result.rows #=> [["hello world"]]


Crystal-pg is tested on Postgres versions 9.1 through 9.4 and developed on 9.5 (travis does not currently have 9.5 support). Since it uses protocal version 3, older versions probably also work but are not guaranteed.

Linking requires that the pg_config binary is in your $PATH and returns correct results for pg_config --includedir and pg_config --libdir.

Supported Datatypes

  • text
  • boolean
  • int8, int2, int4
  • float4, float8
  • timestamptz, date, timestamp (but no one should use ts when tstz exists!)
  • json and jsonb
  • uuid
  • bytea
  • numeric/decimal (1)
  • varchar
  • regtype
  • geo types: point, box, path, lseg, polygon, circle, line

1: A note on numeric: In postgres this type has arbitrary percision. In this driver, it is represented as a PG::Numeric which retians all precision, but if you need to do any math on it, you will probably need to cast it to a float first. If you need true abitrary percision, you can optionally require pg_ext/big_rational which adds #to_big_r, but requires that you have LibGMP installed.

Connection Pooling

If you would like a connection pool, check out ysbaddaden/pool


  • more datatypes (ranges, hstore)
  • more info in postgres exceptions
  • transaction help