<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edgar Pino</title>
    <description>The latest articles on DEV Community by Edgar Pino (@edgar971).</description>
    <link>https://dev.to/edgar971</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F108029%2F0f374238-f9b0-4c92-b53b-0f3f1c4525a4.jpeg</url>
      <title>DEV Community: Edgar Pino</title>
      <link>https://dev.to/edgar971</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edgar971"/>
    <language>en</language>
    <item>
      <title>Getting Started with Ecto Part 1: Installing and Configuring Ecto</title>
      <dc:creator>Edgar Pino</dc:creator>
      <pubDate>Mon, 15 Oct 2018 19:53:11 +0000</pubDate>
      <link>https://dev.to/edgar971/getting-started-with-ecto-part-1-installing-and-configuring-ecto-4b37</link>
      <guid>https://dev.to/edgar971/getting-started-with-ecto-part-1-installing-and-configuring-ecto-4b37</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Ecto?
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Setup
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's add the &lt;code&gt;ecto&lt;/code&gt; and &lt;code&gt;postgrex&lt;/code&gt; packages to our &lt;code&gt;deps&lt;/code&gt; function in the &lt;code&gt;mix.exs&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defp&lt;/span&gt; &lt;span class="n"&gt;deps&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ecto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"~&amp;gt; 2.2"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:postgrex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"~&amp;gt; 0.13.5"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;NOTE: Postgrex is used to execute Ecto queries against our Postgres database.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;mix deps.get&lt;/code&gt; to install our added dependencies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that we have those packages installed, let's configure Ecto. We can do that by running the following &lt;a href="https://hexdocs.pm/ecto/Mix.Tasks.Ecto.html"&gt;generator command&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mix ecto.gen.repo -r GettingStartedWithEcto.Repo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will generate our Repo, used to connect and query our database, in &lt;code&gt;lib/getting_started_with_ecto/repo.ex&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;defmodule GettingStartedWithEcto.Repo do
  use Ecto.Repo, otp_app: :getting_started_with_ecto
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And update our config to connect to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:getting_started_with_ecto&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;GettingStartedWithEcto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Repo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;adapter:&lt;/span&gt; &lt;span class="no"&gt;Ecto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Adapters&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Postgres&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;database:&lt;/span&gt; &lt;span class="s2"&gt;"getting_started_with_ecto_repo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;username:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;password:&lt;/span&gt; &lt;span class="s2"&gt;"pass"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="ss"&gt;hostname:&lt;/span&gt; &lt;span class="s2"&gt;"localhost"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The one thing the generator didn't do is tell our Elixir application about our &lt;code&gt;GettingStartedWithEcto.Repo&lt;/code&gt;.&lt;br&gt;
Add the following line at the end of&lt;code&gt;config.exs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config :getting_started_with_ecto, ecto_repos: [GettingStartedWithEcto.Repo]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;NOTE: Your Postgres configuration might be different.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Double check your username, password, and host if you are having problems connecting.&lt;/li&gt;
&lt;li&gt;The default port of &lt;code&gt;5432&lt;/code&gt; is used, but you can change it by adding to the config above: &lt;code&gt;port: 15432&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last thing we need to do is set up the &lt;code&gt;GettingStartedWithEcto.Repo&lt;/code&gt; as a supervisor within our application's&lt;br&gt;
supervision tree inside &lt;code&gt;lib/getting_started_with_ecto/application.ex&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="no"&gt;GettingStartedWithEcto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Repo&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;strategy:&lt;/span&gt; &lt;span class="ss"&gt;:one_for_one&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;name:&lt;/span&gt; &lt;span class="no"&gt;GettingStartedWithEcto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Supervisor&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;Supervisor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start_link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the database
&lt;/h2&gt;

&lt;p&gt;This last step should be easy if everything is installed and configured properly.&lt;/p&gt;

&lt;p&gt;The following command creates our database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mix ecto.create
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should see the following message if everything was successful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The database for GettingStartedWithEcto.Repo has been created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;GettingStartedWithEcto.Repo&lt;/code&gt; is the repository that handles our database queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Congratulations 🎉 🎉 🎉
&lt;/h2&gt;

&lt;p&gt;You have installed and configured Ecto. On the next post, we will learn how to create migrations and schemas in our Elixir application. &lt;/p&gt;

</description>
      <category>elixir</category>
      <category>ecto</category>
    </item>
  </channel>
</rss>
