Ecto v2.2.11 Ecto.Adapters.Postgres View Source

Adapter module for PostgreSQL.

It uses postgrex for communicating to the database and a connection pool, such as poolboy.

Features

  • Full query support (including joins, preloads and associations)
  • Support for transactions
  • Support for data migrations
  • Support for ecto.create and ecto.drop operations
  • Support for transactional tests via Ecto.Adapters.SQL

Options

Postgres options split in different categories described below. All options should be given via the repository configuration. These options are also passed to the module specified in the :pool option, so check that module’s documentation for more options.

Compile time options

Those options should be set in the config file and require recompilation in order to make an effect.

Connection options

  • :pool - The connection pool module, defaults to DBConnection.Poolboy
  • :pool_timeout - The default timeout to use on pool calls, defaults to 5000
  • :timeout - The default timeout to use on queries, defaults to 15000
  • :hostname - Server hostname
  • :port - Server port (default: 5432)
  • :username - Username
  • :password - User password
  • :ssl - Set to true if ssl should be used (default: false)
  • :ssl_opts - A list of ssl options, see Erlang’s ssl docs
  • :parameters - Keyword list of connection parameters
  • :connect_timeout - The timeout for establishing new connections (default: 5000)
  • :socket_options - Specifies socket configuration

The :socket_options are particularly useful when configuring the size of both send and receive buffers. For example, when Ecto starts with a pool of 20 connections, the memory usage may quickly grow from 20MB to 50MB based on the operating system default values for TCP buffers. It is advised to stick with the operating system defaults but they can be tweaked if desired:

socket_options: [recbuf: 8192, sndbuf: 8192]

We also recommend developers to consult the Postgrex documentation for a complete listing of all supported options.

Storage options

  • :encoding - the database encoding (default: “UTF8”)
  • :template - the template to create the database from
  • :lc_collate - the collation order
  • :lc_ctype - the character classification
  • :dump_path - where to place dumped structures

After connect callback

If you want to execute a callback as soon as connection is established to the database, you can use the :after_connect configuration. For example, in your repository configuration you can add:

after_connect: {Postgrex, :query!, [“SET search_path TO global_prefix”, []]}

You can also specify your own module that will receive the Postgrex connection as argument.

Extensions

Both PostgreSQL and its adapter for Elixir, Postgrex, support an extension system. If you want to use custom extensions for Postgrex alongside Ecto, you must define a type module with your extensions. Create a new file anywhere in your application with the following:

Postgrex.Types.define(MyApp.PostgresTypes,
                      [MyExtension.Foo, MyExtensionBar] ++ Ecto.Adapters.Postgres.extensions(),
                      json: Poison)

Once your type module is defined, you can configure the repository to use it:

config :my_app, MyApp.Repo, types: MyApp.PostgresTypes

Link to this section Summary

Functions

All Ecto extensions for Postgrex

Link to this section Functions

All Ecto extensions for Postgrex.