How to Create Database Seed Data in Elixir Phoenix

From ElixirBlocks
Jump to: navigation, search

This page is in progress

Before you Begin

Installing Phoenix

This article assumes that you have installed the Phoenix web framework and all its dependencies correctly without errors. If you have not installed the Phoenix web framework please view the documentation here


Getting Started

This article demonstrates how to seed PostgreSQL table data in Elixir Phoenix. Seed data is useful when experimenting with Elixir database commands. To begin, you must first create a basic phoenix application.


Select a directory to create you app:

Run the command

mix phx.new app

The command line prompts you to install dependencies. Select Y for yes.

Create a Database

To create a database you run the mix ecto.create command. Before you execute the command, your Phoenix app needs to be configured to communicate with your database. The default database that Phoenix uses is PostgreSQL.

The following code in

 app/config/dev.exs 

contains the basic configuration values needed to interact with the database. The database field contains the name of the database and if it doesn't exist Phoenix will create it.


config :app, App.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "app_dev",

To create the database run this:

mix ecto.create

When the command is done executing you can create a table using the following code:



mix phx.gen.context Todos Todo todos title:string done:boolean

When complete you are prompted to run mix.ecto.migrate. Do that now.

mix ecto.migrate