Postgrex.Connection

Main API for Postgrex. This module handles the connection to postgres.

Source

Summary

listen!(pid, channel, opts \\ [])

Listens to an asynchronous notification channel channel. See listen/2

listen(pid, channel, opts \\ [])

Listens to an asynchronous notification channel using the LISTEN command. A message {:notification, connection_pid, ref, channel, payload} will be sent to the calling process when a notification is received

parameters(pid, opts \\ [])

Returns a cached map of connection parameters

query!(pid, statement, params, opts \\ [])

Runs an (extended) query and returns the result or raises Postgrex.Error if there was an error. See query/3

query(pid, statement, params, opts \\ [])

Runs an (extended) query and returns the result as {:ok, %Postgrex.Result{}} or {:error, %Postgrex.Error{}} if there was an error. Parameters can be set in the query as $1 embedded in the query string. Parameters are given as a list of elixir values. See the README for information on how Postgrex encodes and decodes elixir values by default. See Postgrex.Result for the result data

start_link(opts)

Start the connection process and connect to postgres

stop(pid, opts \\ [])

Stop the process and disconnect

unlisten!(pid, ref, opts \\ [])

Stops listening on the given channel by passing the reference returned from listen/2

unlisten(pid, ref, opts \\ [])

Stops listening on the given channel by passing the reference returned from listen/2

Functions

listen(pid, channel, opts \\ [])

Specs:

Listens to an asynchronous notification channel using the LISTEN command. A message {:notification, connection_pid, ref, channel, payload} will be sent to the calling process when a notification is received.

Options

  • :timeout - Call timeout (default: infinity)
Source
listen!(pid, channel, opts \\ [])

Specs:

Listens to an asynchronous notification channel channel. See listen/2.

Source
parameters(pid, opts \\ [])

Specs:

Returns a cached map of connection parameters.

Options

  • :timeout - Call timeout (default: infinity)
Source
query(pid, statement, params, opts \\ [])

Specs:

Runs an (extended) query and returns the result as {:ok, %Postgrex.Result{}} or {:error, %Postgrex.Error{}} if there was an error. Parameters can be set in the query as $1 embedded in the query string. Parameters are given as a list of elixir values. See the README for information on how Postgrex encodes and decodes elixir values by default. See Postgrex.Result for the result data.

A type hinted query is run if both the options :param_types and :result_types are given. One client-server round trip can be saved by providing the types to Postgrex because the server doesn’t have to be queried for the types of the parameters and the result.

Options

  • :timeout - Call timeout (default: infinity)
  • :param_types - A list of type names for the parameters
  • :result_types - A list of type names for the result rows

Examples

Postgrex.Connection.query(pid, "CREATE TABLE posts (id serial, title text)", [])

Postgrex.Connection.query(pid, "INSERT INTO posts (title) VALUES ('my title')", [])

Postgrex.Connection.query(pid, "SELECT title FROM posts", [])

Postgrex.Connection.query(pid, "SELECT id FROM posts WHERE title like $1", ["%my%"])

Postgrex.Connection.query(pid, "SELECT $1 || $2", ["4", "2"],
                          param_types: ["text", "text"], result_types: ["text"])
Source
query!(pid, statement, params, opts \\ [])

Specs:

Runs an (extended) query and returns the result or raises Postgrex.Error if there was an error. See query/3.

Source
start_link(opts)

Specs:

Start the connection process and connect to postgres.

Options

  • :hostname - Server hostname (default: PGHOST env variable, then localhost);
  • :port - Server port (default: 5432);
  • :database - Database (required);
  • :username - Username (default: PGUSER env variable, then USER env var);
  • :password - User password (default PGPASSWORD);
  • :encoder - Custom encoder function;
  • :decoder - Custom decoder function;
  • :formatter - Function deciding the format for a type;
  • :parameters - Keyword list of connection parameters;
  • :timeout - Connect timeout in milliseconds (default: infinity);
  • :ssl - Set to true if ssl should be used (default: false);
  • :ssl_opts - A list of ssl options, see ssl docs;
  • :async_connect - Set to true if start_link should return before the connection is completed (default: false);

Function signatures

@spec encoder(info :: TypeInfo.t, default :: fun, param :: term) ::
      binary
@spec decoder(info :: TypeInfo.t, default :: fun, bin :: binary) ::
      term
@spec formatter(info :: TypeInfo.t) ::
      :binary | :text | nil
Source
stop(pid, opts \\ [])

Specs:

Stop the process and disconnect.

Options

  • :timeout - Call timeout (default: infinity)
Source
unlisten(pid, ref, opts \\ [])

Specs:

Stops listening on the given channel by passing the reference returned from listen/2.

Options

  • :timeout - Call timeout (default: infinity)
Source
unlisten!(pid, ref, opts \\ [])

Specs:

  • unlisten!(pid, reference, Keyword.t) :: :ok

Stops listening on the given channel by passing the reference returned from listen/2.

Source