Postgrex.Connection

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

Source

Summary

begin!(pid, opts \\ [])

Starts a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See begin/1

begin(pid, opts \\ [])

Starts a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. Transactions can be nested with the help of savepoints. A transaction won’t end until a rollback/1 or commit/1 have been issued for every begin/1

commit!(pid, opts \\ [])

Commits a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See commit/1

commit(pid, opts \\ [])

Commits a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. See begin/1 for more information

in_transaction(pid, opts \\ [], fun)

Helper for creating reliable transactions. If an error is raised in the given function the transaction is rolled back, otherwise it is commited. A transaction can be cancelled with throw :postgrex_rollback. If there is a connection error Postgrex.Error will be raised. Do not use this function in conjunction with begin/1, commit/1 and rollback/1

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

rollback!(pid, opts \\ [])

Rolls back a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See rollback/1

rollback(pid, opts \\ [])

Rolls back a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. See begin/1 for more information

start_link(opts)

Start the connection process and connect to postgres

stop(pid, opts \\ [])

Stop the process and disconnect

Functions

begin(pid, opts \\ [])

Specs:

Starts a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. Transactions can be nested with the help of savepoints. A transaction won’t end until a rollback/1 or commit/1 have been issued for every begin/1.

Options

  • :timeout - Call timeout (default: infinity)

Examples

# Transaction begun
Postgrex.Connection.begin(pid)
Postgrex.Connection.query(pid, "INSERT INTO comments (text) VALUES ('first')")

# Nested subtransaction begun
Postgrex.Connection.begin(pid)
Postgrex.Connection.query(pid, "INSERT INTO comments (text) VALUES ('second')")

# Subtransaction rolled back
Postgrex.Connection.rollback(pid)

# Only the first comment will be commited because the second was rolled back
Postgrex.Connection.commit(pid)
Source
begin!(pid, opts \\ [])

Specs:

Starts a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See begin/1.

Source
commit(pid, opts \\ [])

Specs:

Commits a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. See begin/1 for more information.

Options

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

Specs:

Commits a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See commit/1.

Source
in_transaction(pid, opts \\ [], fun)

Specs:

  • in_transaction(pid, Keyword.t, (() -> term)) :: term

Helper for creating reliable transactions. If an error is raised in the given function the transaction is rolled back, otherwise it is commited. A transaction can be cancelled with throw :postgrex_rollback. If there is a connection error Postgrex.Error will be raised. Do not use this function in conjunction with begin/1, commit/1 and rollback/1.

Options

  • :timeout - Call timeout (default: infinity). Note that it is not the maximum timeout of the entire call but rather the timeout of the commit/2 and rollback/2 calls that this function makes.
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 TABLES 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 $1 + $2", [40, 2]")

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
rollback(pid, opts \\ [])

Specs:

Rolls back a transaction. Returns :ok or {:error, %Postgrex.Error{}} if an error occurred. See begin/1 for more information.

Options

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

Specs:

Rolls back a transaction. Returns :ok if it was successful or raises Postgrex.Error if an error occurred. See rollback/1.

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;
  • :connect_timeout - Connect timeout in milliseconds (default: 5000);
  • :ssl - Set to true if ssl should be used (default: false);
  • :ssl_opts - A list of ssl options, see ssl docs;

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