Rails / PostgreSQL ================== .. include:: beta.rst This quick-start will go through the steps of: * Creating a `Rails `_ bookmarking app * Connecting it to `ElephantSQL Postgres `_, a leading database-as-a-service provider * Deploying the app on the drie push platform We will assume that you've fulfilled these prerequisites: * `Rails `_ installed on your local machine. * `ElephantSQL Postgres instance `_ with a user and a database already set up. 1. Create a new Rails app ------------------------- We will be creating an app that stores your favourite bookmarks. Let's create a project called ``drie-bookmarker-postgresql``:: $ rails new drie-bookmarker-postgresql --database=postgresql $ cd drie-bookmarker-postgresql $ spring stop # run this because sometimes it blocks rails g 2. Create your bookmark model ----------------------------- We will cheat a bit and generate the bookmark model with Rails scaffold:: $ rails g scaffold bookmark name:string url:string 3. Edit database.yml config file -------------------------------- :: $ vi config/database.yml We need to modify database.yml to connect to the ElephantSQL PostgreSQL database. Please delete all the lines from database.yml and replace it with these lines:: production: url: <%= ENV['DATABASE_URI'] %> sslmode: "require" This is a very basic config to connect to your ElephantSQL PostgreSQL database. We've set that the database connection between our drie push Rails bookmarker app and Elephant to use SSL. For more information about ElephantSQL PostgreSQL database-as-a-service, please check out: * `ElephantSQL documentation `_ Notice that we load the ``DATABASE_URI`` from environment variables. We will set that in `step 8 `_. 4. Set the local DATABASE_URI environment variable -------------------------------------------------- You will need to get the database URI from `ElephantSQL `_. Log into your `ElephantSQL account `_ and go to: https://customer.elephantsql.com/instance Click the "Details" button. On the following page, you should see your ElephantSQL Postgres database connection string there. The URI pattern should be ``postgres://:@someserver.elephantsql.com:portnum/`` To set the local ``DATABASE_URI`` environment variable, run the command below, replacing the placeholders for ``dbuser``, ``dbpassword``, ``someserver``, ``portnum`` and ``dbname``:: $ export DATABASE_URI=postgres://dbuser:dbpassword@someserver.elephantsql.com:portnum/dbname 5. Do a Rails database migration -------------------------------- :: $ rake db:migrate RAILS_ENV=production This will create the tables for this app on ElephantSQL. 6. Create a local git repo and set it up to deploy to drie push --------------------------------------------------------------- :: $ git init $ git add -f * $ git commit -m "Created drie bookmarker app and setting up to deploy to drie." After you've created your local git repo, we now set up the remote drie push git repo where we push our code.:: $ app_name=$(curl -s http://m.rang.app.push.drieapp.co) # generates a random app name for you $ echo "Your app is called ${app_name}" # you can see what app name was generated for you $ git remote add drie ${app_name}.app@push.drieapp.co:code.git 7. Deploy you code to drie push ------------------------------- :: $ git push --set-upstream drie master This pushes up your code to drie push where it builds it on the remote server. 8. Set the DATABASE_URI environment variable in drie push --------------------------------------------------------- To set the ``DATABASE_URI`` environment variable in drie push, run the command below, replacing the placeholders for ``dbuser``, ``dbpassword``, ``someserver``, ``portnum`` and ``dbname``:: $ ssh ${app_name}.app@push.drieapp.co configure environment -s DATABASE_URI=postgres://dbuser:dbpassword@someserver.elephantsql.com:portnum/dbname 9. Confirm that your bookmarking app works ------------------------------------------ :: $ curl http://master.${app_name}.app.push.drieapp.co/bookmarks You should see an empty list where you can add new bookmarks. Congratulations! You've successfully deployed your Rails / ElephantSQL PostgreSQL app on drie push!