rainforestapp/postgrex

Name: postgrex

Owner: Rainforest QA

Description: PostgreSQL driver for Elixir

Created: 2016-08-30 22:17:52.0

Updated: 2016-08-30 22:17:54.0

Pushed: 2016-08-30 15:57:14.0

Homepage: http://hexdocs.pm/postgrex/

Size: 1100

Language: Elixir

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Postgrex

Build Status

PostgreSQL driver for Elixir.

Documentation: http://hexdocs.pm/postgrex/

Example
 {:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")
, #PID<0.69.0>}
 Postgrex.query!(pid, "SELECT user_id, text FROM comments", [])
tgrex.Result{command: :select, empty?: false, columns: ["user_id", "text"], rows: [[3,"hey"],[4,"there"]], size: 2}}
 Postgrex.query!(pid, "INSERT INTO comments (user_id, text) VALUES (10, 'heya')", [])
tgrex.Result{command: :insert, columns: nil, rows: nil, num_rows: 1}}
Disclaimer

Postgrex is currently on 0.x beta releases. We are heading towards a stable release soon. Please consult the issues tracker for more information and outstanding issues.

Features
Data representation
PostgreSQL      Elixir
----------      ------
NULL            nil
bool            true | false
char            "é"
int             42
float           42.0
text            "eric"
bytea           <<42>>
numeric         #Decimal<42.0> *
date            %Postgrex.Date{year: 2013, month: 10, day: 12}
time(tz)        %Postgrex.Time{hour: 0, min: 37, sec: 14, usec: 0} **
timestamp(tz)   %Postgrex.Timestamp{year: 2013 month: 10, day: 12, hour: 0, min: 37, sec: 14, usec: 0} **
interval        %Postgrex.Interval{months: 14, days: 40, secs: 10920}
array           [1, 2, 3]
composite type  {42, "title", "content"}
range           %Postgrex.Range{lower: 1, upper: 5}
uuid            <<160,238,188,153,156,11,78,248,187,109,107,185,189,56,10,17>>
hstore          %{"foo" => "bar"}
oid types       42

* Decimal

** Timezones will always be normalized to UTC or assumed to be UTC when no information is available, either by PostgreSQL or Postgrex

Postgrex does not automatically cast between types. For example, you can't pass a string where a date is expected. To add type casting, support new types, or change how any of the types above are encoded/decoded, you can use extensions.

Extensions

Extensions are used to extend Postgrex' built-in type encoding/decoding.

Here is a JSON extension that supports encoding/decoding Elixir maps to the Postgres' JSON type.

To use the extension pass it to the connection as seen below:

grex.start_link(extensions: [{Postgrex.Extensions.JSON, library: Poison}], ...)
OID type encoding

PostgreSQL's wire protocol supports encoding types either as text or as binary. Unlike most client libraries Postgrex uses the binary protocol, not the text protocol. This allows for efficient encoding of types (e.g. 4-byte integers are encoded as 4 bytes, not as a string of digits) and automatic support for arrays and composite types.

Unfortunately the PostgreSQL binary protocol transports OID types as integers while the text protocol transports them as string of their name, if one exists, and otherwise as integer.

This means you either need to supply oid types as integers or perform an explicit cast (which would be automatic when using the text protocol) in the query.

ils since $1 is regclass not text.
y("select nextval($1)", ["some_sequence"])

rform an explicit cast, this would happen automatically when using a
ient library that uses the text protocol.
y("select nextval($1::text::regclass)", ["some_sequence"])

termine the oid once and store it for later usage. This is the most
ficient way, since PostgreSQL only has to perform the lookup once. Client
braries using the text protocol do not support this.
ws: [{sequence_oid}]} = query("select $1::text::regclass", ["some_sequence"])
y("select nextval($1)", [sequence_oid])
PgBouncer

When using PgBouncer with transaction or statement pooling named prepared queries can not be used because the bouncer may route requests from the same postgrex connection to different PostgreSQL backend processes and discards named queries after the transactions closes. To force unnamed prepared queries:

grex.start_link(prepare: :unnamed)
Contributing

To contribute you need to compile Postgrex from source and test it:

t clone https://github.com/elixir-ecto/postgrex.git
 postgrex
x test

The tests requires some modifications to your hba file. The path to it can be found by running $ psql -U postgres -c "SHOW hba_file" in your shell. Put the following above all other configurations (so that they override):

    all             postgrex_md5_pw         127.0.0.1/32    md5
    all             postgrex_cleartext_pw   127.0.0.1/32    password

The server needs to be restarted for the changes to take effect. Additionally you need to setup a Postgres user with the same username as the local user and give it trust or ident in your hba file. Or you can export $PGUSER and $PGPASSWORD before running tests.

Testing hstore on 9.0

Postgres versions 9.0 does not have the CREATE EXTENSION commands. This means we have to locate the postgres installation and run the hstore.sql in contrib to install hstore. Below is an example command to test 9.0 on OS X with homebrew installed postgres:

VERSION=9.0 PGPATH=/usr/local/share/postgresql9/ mix test
License

Copyright 2013 Eric Meadows-Jönsson

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.