DEV Community

Edgar Pino
Edgar Pino

Posted on

Getting Started with Ecto Part 1: Installing and Configuring Ecto

Welcome to part one of Getting Started with Ecto. 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 and postgrex packages to our deps function in the mix.exs file:
defp deps do
      {:ecto, "~> 2.2"},
      {:postgrex, "~> 0.13.5"}

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

And update our config to connect to the database:

config :getting_started_with_ecto, GettingStartedWithEcto.Repo,
  adapter: Ecto.Adapters.Postgres,
  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 = [
    opts = [strategy: :one_for_one, name: GettingStartedWithEcto.Supervisor]
    Supervisor.start_link(children, opts)

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.

Discussion (10)

edgar971 profile image
Edgar Pino Author

The active record pattern is an architectural pattern that stores in-memory object data in relational databases. For example, Active Record ORM in Ruby, Entity Framework in C#, and Sequelize in Node.js.

chenge profile image

Do you have a tutorial for Ecto with sqlite or Elixir's own db?

ranska profile image

In this article we configure things for the "supervision tree".
Did you have some basic links or articles to explain what is it.
Maybe as a beginner I don't need it now,
but just a little introduction can be interesting.

chenge profile image
chenge • Edited
** (Mix) The database for GettingStartedWithEcto.Repo couldn't be created: ERROR 42501 (insufficient_privilege): permission denied to create database

?, need help! thanks.

ranska profile image

Last day I go same error and I create an postgres user with privilege and it work.
May be it come from your db.

edgar971 profile image
Edgar Pino Author

I agree, Ecto is not an ORM. It doesn't follow the Active Record pattern like other database tools.

chenge profile image

I mean mnesia.