Edgar Pino

Getting Started with Ecto Part 1: Installing and Configuring Ecto

September 30, 2018

Note: This post was updated to work with Ecto 3.0

Welcome to part one of Getting Started with Ecto 3.0. This post is part of a series to teach you how to use Ecto. I will go over how to setup Ecto, create migrations and schemas, along with simple and more complicated queries. By the end of the series, you will be able to comfortably use Ecto with Postgres in your Elixir applications.

What is Ecto?

Ecto is a database package for Elixir applications. It is used for writing database queries and interacting with your database. With Ecto you can create migrations, define schemas, create and update records, and query your database. The current version supports PostgreSQL and MySQL. Support for MSSQL, SQLite, and MongoDB will be added in the future.

Installation and Setup

Let’s start by adding and configuring Ecto in our Elixir application. Feel free to skip this step if you have done this already, or if you are using Phoenix.

  1. Let’s add the ecto_sql and postgrex packages to our deps function in the mix.exs file:
defp deps do
    [
      {:ecto_sql, "~> 3.0"},
      {:postgrex, ">= 0.0.0"}
    ]
end

NOTE: Postgrex is used to execute Ecto queries against our Postgres database.

  1. Run mix deps.get to install our added dependencies.

Now that we have those packages installed, let’s configure Ecto. We can do that by running the following generator command:

mix ecto.gen.repo -r GettingStartedWithEcto.Repo

This will generate our Repo, used to connect and query our database, in lib/getting_started_with_ecto/repo.ex:

defmodule GettingStartedWithEcto.Repo do
  use Ecto.Repo,
    otp_app: :getting_started_with_ecto,
    adapter: Ecto.Adapters.Postgres
end

And update our config to connect to the database:

config :getting_started_with_ecto, GettingStartedWithEcto.Repo,
  database: "getting_started_with_ecto_repo",
  username: "user",
  password: "pass",
  hostname: "localhost"

The one thing the generator didn’t do is tell our Elixir application about our GettingStartedWithEcto.Repo. Add the following line at the end ofconfig.exs:

config :getting_started_with_ecto, ecto_repos: [GettingStartedWithEcto.Repo]

NOTE: Your Postgres configuration might be different.

  1. Double check your username, password, and host if you are having problems connecting.
  2. The default port of 5432 is used, but you can change it by adding to the config above: port: 15432

The last thing we need to do is set up the GettingStartedWithEcto.Repo as a supervisor within our application’s supervision tree inside lib/getting_started_with_ecto/application.ex:

def start(_type, _args) do
    children = [
      GettingStartedWithEcto.Repo
    ]
    opts = [strategy: :one_for_one, name: GettingStartedWithEcto.Supervisor]
    Supervisor.start_link(children, opts)
end

Creating the database

This last step should be easy if everything is installed and configured properly.

The following command creates our database:

mix ecto.create

You should see the following message if everything was successful:

The database for GettingStartedWithEcto.Repo has been created

The GettingStartedWithEcto.Repo is the repository that handles our database queries.

Congratulations 🎉 🎉 🎉

You have installed and configured Ecto. On the next post, we will learn how to create migrations and schemas in our Elixir application.


Edgar Pino

Software Engineer @Pluralsight. Interested in distributed systems, machine learning, and the web. Follow me on Twitter.