Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Advice on Pre-Populating PostgreSQL Table with Ecto

TL;DR When playing with Ecto, where (in the code) should I load data to pre-populate a small table after it is created?

I'm learning Elixir, Phoenix, and Ecto. I have a table in my schema to define a list of configurable options. I'd like to pre-populate this table with a minimum set of options when the table is created.

It seems to me that the _create_table.exs script is the place to do that since that's where the table is created. I was looking at an execute{} block but that seems klunky.

Is there a better place in the code to accomplish pre-populating a new table?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

In Phoenix, the common way to populate the DB is to use priv/repo/seeds.exs it generates with mix run task as:

mix run priv/repo/seeds.exs

But this approach does actually work pretty fine with any Ecto project. Simply create the file, put seeding code there, and update your mix.exs aliases section as

defp aliases do
  [
    "ecto.setup": [
      "ecto.create",
      "ecto.migrate",
      "run priv/repo/seeds.exs"
    ],
    ...
  ]

Now mix ecto.setup would create, migrate, and fill in the database.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!