Search code examples
elixirphoenix-frameworkecto

Ecto.migrate create database schema


I am just getting started with Elixir/Phoenix and Ecto migrations. I cannot find a way to create Postgres database schemas using migrations. My database will need up to 4 different schemas in the near future. I mean doing this, but creating a schema instead of a table. I'm not sure it can be done, but if so, how? Thanks.

  def change do
    create table(:people) do
      ...
    end
  end

Solution

  • The Ecto.Migration.execute/1 function is what you're looking for. You can use it to execute arbitrary SQL commands, like creating users, schemas, or adding extensions to the database.

    defmodule Example.Migrations.MakeSchema do
      use Ecto.Migration
    
      execute "CREATE SCHEMA example"
    end