Rails + Postgres Guide
Datica Rails - PostgreSQL Guide
This guide will walk you through an end-to-end roll-out of a Ruby on Rails app with logging, environment variables and connection to a PostgreSQL database hosted on Datica’s Platform. We assume that you already have a contract in place with us as well as a signed BAA. Please contact us if you need help or register for a Free Trial.
You will start by creating your environment and linking your local app to our internal git repositories as outlined in our Onboarding Guide. Then you will add logging, environment variables and a PostgreSQL database to your app. Lastly you will commit your changes and push your app to the secure Datica platform.
Setting up your local Rails app
We created an example Ruby on Rails app that connects to a PostgreSQL database to use as an example for this guide. This example was created by running
rails new ruby-example-app --database=postgresql.
The example app does a few basic things:
- Connects to a PostgreSQL database
- Has a single route which both stores a counter and a timestamp in the database and also displays the most recent ten records
- Sends some example logs that will show up in the Datica environment’s logging service
Feel free to use this example application as a base for your own, or just as a reference while you are developing. If you want more help with developing your application with Rails, the Ruby on Rails Getting Started guide is very useful.
Adding logging to your Rails app
The Datica logging service keeps track of everything that is sent to STDOUT and STDERR, so the easiest way to add messages to your logs are to create loggers using standard Ruby Logger practices:
Logger.new(STDERR). More information on how logging works in Datica environments is located here.
On-boarding and SSL/SSH setup
Once your local Rails application is up and running, you will need to complete the Datica onboarding process for your environment. The steps include setting up SSL Certificates, SSH Keys, and environment variables to ensure your environment can be built appropriately.
Readying your application for deployment on Datica
There are a few things that need to be changed in order for your application to run appropriately on Datica. These steps have already been handled in the Ruby on Rails example application.
- Create a file called “Procfile” which includes the following, which tells Datica how to run your application. The $PORT variable is automatically generated by Datica and must be present, otherwise your application will not run properly.
web: bundle exec rails server -p $PORT
- Update your database connection information in config/database.yml to use the $DATABASE_URL environment variable that Datica automatically generates. Make sure you change the value of YOUR_DATABASE_NAME to the actual name of the database you want to connect to.
production: url: <%= ENV['DATABASE_URL'] %> database: YOUR_DATABASE_NAME
- Ensure your application is committed into a git repository.
$ git init $ git add . $ git commit -m "Initial commit"
Using the Datica CLI to connect your app to The Platform
Once Datica has provisioned your environment and you have made the appropriate changes to your application, you can initialize your local git repository to your Datica code service, which will enable you to push your application to Datica. There is a Datica Platform CLI available which makes interacting with your environment quite easy.
Once you have the Datica CLI installed, run the following command from within your application’s directory. This signs you into the Datica Platform and adds a git remote to your local repository, which will allow you to push your code
$ datica init
Pushing your application to Datica’s Platform
To push your code to Datica, run the command below from within your application’s directory.
$ git push datica master
Preparing PostgreSQL for your application
While you are waiting for Datica to complete your initial deployment, you can set up your database and run appropriate migrations so that your application can connect to PostgreSQL. The Datica Platform CLI can be used to connect to your database and run commands. Below is an example of how to run db:setup on PostgreSQL to create your database, tables, etc. Make sure you change the value of YOUR_DATABASE_NAME to the actual name of the database you want to connect to. Note: your database service may be named something other than “db01” if you changed it in the Datica Dashboard.
$ datica -E "<your_env_name>" rake <service_name> db:setup