docs/content/stable/develop/tutorials/build-apps/elixir/cloud-ysql-elixir.md
The following tutorial shows a small Elixir application that connects to a YugabyteDB cluster using the Elixir Postgrex driver and performs basic SQL operations. Use the application as a template to get started with YugabyteDB Aeon in Elixir.
The latest versions of Elixir, Erlang VM, IEx and Mix (tested with Elixir 1.17.1 and Erlang/OTP 26 erts-14.2.5).
Clone the sample application to your computer:
git clone https://github.com/YugabyteDB-Samples/yugabytedb-simple-elixir-app.git && cd yugabytedb-simple-elixir-app/postgrex/simple_app
If your cluster is running on YugabyteDB Aeon, you need to modify the connection parameters so that the application can establish a connection to the YugabyteDB cluster. (You can skip this step if your cluster is running locally and listening on 127.0.0.1:5433.)
To do this:
Open the lib/simple_app.ex file.
Set the following configuration parameter constants:
yugabyte).yugabyte and yugabyte). For YugabyteDB Aeon, use the credentials in the credentials file you downloaded.Save the file.
First, add all required dependencies.
mix deps.get
Compile the application and start an IEx session:
iex -S mix
Start the application.
SimpleApp.start
You should see output similar to the following:
>>>> Successfully connected to YugabyteDB! PID: #PID<0.221.0>
>>>> Successfully created table DemoAccount.
>>>> Selecting accounts:
["Jessica", 28, "USA", 10000]
["John", 28, "Canada", 9000]
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
["Jessica", 28, "USA", 9200]
["John", 28, "Canada", 9800]
:ok
You have successfully executed a basic Elixir application that works with YugabyteDB.
Open the simple_app.ex file in the yugabytedb-simple-elixir-app/postgrex/simple_app/lib folder to review the methods.
The start method establishes a connection with your cluster via the Elixir Postgrex driver and executes the rest of the application logic.
db_config = @db_config
{:ok, pid} = Postgrex.start_link(db_config)
The create_database method creates a sample table and loads it with a few records.
Postgrex.query!(pid, "DROP TABLE IF EXISTS DemoAccount", [])
Postgrex.query!(pid, """
CREATE TABLE DemoAccount (
id int PRIMARY KEY,
name varchar,
age int,
country varchar,
balance int)
""",[])
Postgrex.query!(pid, """
INSERT INTO DemoAccount VALUES
(1, 'Jessica', 28, 'USA', 10000),
(2, 'John', 28, 'Canada', 9000)
""",[])
The select_accounts method queries the data from the database.
result = Postgrex.query!(pid,
"SELECT name, age, country, balance FROM DemoAccount", [])
Enum.each(result.rows, fn row ->
IO.inspect(row)
end)
The transfer_money method updates your data consistently with distributed transactions.
Postgrex.transaction(pid, fn(conn) ->
# Deduct amount from Jessica's account
Postgrex.query!(conn,
"UPDATE DemoAccount SET balance = balance - $1 WHERE name = 'Jessica'", [amount])
# Add amount to John's account
Postgrex.query!(conn,
"UPDATE DemoAccount SET balance = balance + $1 WHERE name = 'John'", [amount])
end)
YugabyteDB Community Open Hours - featuring Chris McCord, the creator of the Phoenix framework, demonstrating how to build multi-region applications using Phoenix, Fly.io, and YugabyteDB.