Programador ajudante e aprendiz da comunidade open source.

CRUD in 5 Minutes with Phoenix and Elixir

I know, you already heard something like this in the Rails world. But for somebody that never created an application with Phoenix and Elixir, it is a good point to start.


Elixir is a new language created by José Valim. It's a functional language that makes things safer and this language has a framework called Phoenix that help us with HTTP requests and so.


First of all, you need to install the Erlang/Elixir package manager. In this world, try to use the asdf that you can install via brew:

brew update
brew install asdf

Now just check the last version of Erlang and install it globally:

asdf install erlang 24.0
asdf global erlang 24.0

Now install the last version of Elixir:

asdf install elixir 1.12
asdf global elixir 1.12

Check your versions:

elixir --version

# Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

# Elixir 1.12.0 (compiled with Erlang/OTP 22)

Our dependencies package manager calls Hex so let's install it:

mix local.hex

And finally, we can install the Phoenix Framework:

mix archive.install hex phx_new 1.5.9

Creating the project

I'll create the project skeleton skipping the dependencies install since we want to use Yarn over NPM:

mix bible
cd bible

Now we can configure and run the project:

# installs the dependencies
mix deps.get

# creates the database
mix ecto.setup

# installs assets
cd assets && yarn install && cd -

# run the server
mix phx.server

Visit the URL and see your project running: localhost:4000

Configuring Database

You'll need to install Docker and then create a docker file:

# docker-file.yml

version: "3.8"


    image: postgres:11-alpine

      - 5432:5432

Since we won't use a password in dev and test, let's set it to empty:

# config/dev.exs

config :bible, Bible.Repo,
  username: "postgres",
  password: "",
# config/test.exs

config :bible, Bible.Repo,
  username: "postgres",
  password: "",

You're good to boot up your database:

docker-compose up -d

Creating an automatic CRUD

In Phoenix, we separate the code inside Modules, so the Persons will be a module containing our CRUD code.
Person is the model name and persons in the table with name and description as fields.

mix phx.gen.html Persons Person persons name:string description:text

Enable the CRUD routes adding the following code in the final of the block scope "/", BibleWeb do:

scope "/", BibleWeb do

  resources "/persons", PersonController

You should apply the migration in the database:

mix ecto.migrate

Just visit localhost:4000/persons and have fun!


Elixir and Phoenix are funny and strong worlds and we already have a lot of tools around this community. I believe that this language will be the future and will replace a couple of Java-like systems in big companies, mainly financial ones. What do you think?

Repository link:

Em resposta:
Formate seu código utilizando Markdown.