Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 1, 2020 07:15 pm GMT

Setting up Rails and PostgreSQL

When building a production-ready application with Rails, the most popular database to use is PostgreSQL. However, Rails via ActiveRecord does support a few databases out of the box. These databases include PostgreSQL, MySQL, and SQLite3. Rails can support other databases through gems and their ORM.

Since it is most likely you are using both PostgreSQL and MacOS(OSX), I recommend just installing Postgres.app. Simply head to the postgres.app site. Then download and follow the simple instructions to have a PostgreSQL server and library installed on your box.

Another option you may use if you are skilled in the terminal is to install through Brew. Once you have Brew on your system, you can run brew install postgresql and follow the post install prompts.

rails new with pg

By default, Rails uses SQLite3 for development databases. This database type is used to ease install and development. However, since you are building an app that you intend to deploy, you are going to use PostgreSQL in all of your environments. When initializing a new Rails app, it is quite easy to change the default database type or other default settings.

rails new app_name -T --database=postgresql

After a minute or two, Rails will have installed all the required files for the base application. Rails then installed the base gems and ran a few other shell commands to get your application started.

Database Configuration

If everything in the install went correctly, you would be able to use a Rails command to create the database on your local PostgreSQL instance.

To be safe, take a look at the database configuration file and see what is in there. The database configuration file config/database.yml comes with information about installing the pg gem. Along with other option settings, you may change if you deviate at any point with your PostgreSQL setup. Here is the relevant development connection information:

default: &default  adapter: postgresql  encoding: unicode  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>development:  <<: *default  database: app_name_development

Here Rails is setting up a shared configuration between all environments setting the adapter type, encoding, and connection pool settings. None of this is relevant to the default usage of Rails, but you may need to change this at some point in your application's lifetime.

The development block specifies to use the default settings and also use a database_name of app_name_development. The default naming for Rails databases is appname_environment. Meaning your test database name would be app_name_test and so on.

Now, let's change our directory to that of the Rails app and create the database for Rails to use.

cd app_namerails db:create

Note: This tutorial is an excerpt from a chapter in my upcoming book Build A SaaS App in Rails 6. The book guides you from humble beginnings by deploying an app to production. The book is now in pre-sale, and you can grab a free chapter right now!


Original Link: https://dev.to/rob__race/setting-up-rails-and-postgresql-5c0k

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To