Postgrex.Connection
Main API for Postgrex. This module handles the connection to postgres.
Summary↑
| listen!(pid, channel, opts \\ []) | Listens to an asynchronous notification channel |
| listen(pid, channel, opts \\ []) | Listens to an asynchronous notification channel using the |
| 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 |
| query(pid, statement, params, opts \\ []) | Runs an (extended) query and returns the result as |
| rebootstrap(pid, opts \\ []) | |
| 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
|
| unlisten(pid, ref, opts \\ []) | Stops listening on the given channel by passing the reference returned from
|
Functions
Specs:
- listen(pid, String.t, Keyword.t) :: {:ok, reference} | {:error, Postgrex.Error.t}
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:5000)
Specs:
Listens to an asynchronous notification channel channel. See listen/2.
Specs:
- parameters(pid, Keyword.t) :: %{}
Returns a cached map of connection parameters.
Options
:timeout- Call timeout (default:5000)
Specs:
- query(pid, iodata, list, Keyword.t) :: {:ok, Postgrex.Result.t} | {:error, Postgrex.Error.t}
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.
Options
:timeout- Call timeout (default:5000)
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%"])
Specs:
- query!(pid, iodata, list, Keyword.t) :: Postgrex.Result.t
Runs an (extended) query and returns the result or raises Postgrex.Error if
there was an error. See query/3.
Specs:
- start_link(Keyword.t) :: {:ok, pid} | {:error, Postgrex.Error.t | term}
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);:parameters- Keyword list of connection parameters;:timeout- Connect timeout in milliseconds (default:5000);:ssl- Set totrueif ssl should be used (default:false);:ssl_opts- A list of ssl options, see ssl docs;:async_connect- Set totrueifstart_linkshould return before the connection is completed (default:false);:extensions- A list of{module, opts}pairs wheremoduleis implementing thePostgrex.Extensionbehaviour andoptsare the extension options;
Specs:
- stop(pid, Keyword.t) :: :ok
Stop the process and disconnect.
Options
:timeout- Call timeout (default:5000)
Specs:
- unlisten(pid, reference, Keyword.t) :: :ok | {:error, Postgrex.Error.t}
Stops listening on the given channel by passing the reference returned from
listen/2.
Options
:timeout- Call timeout (default:5000)
Specs:
- unlisten!(pid, reference, Keyword.t) :: :ok
Stops listening on the given channel by passing the reference returned from
listen/2.