Creating a Rails Projects with Postgres

Although this may seem a short read, as developers we may forget how to do the simplest things like creating a new rails project with sqlite3 or PostgreSQL database management systems. We can do this several ways, but there is one way that is going to make your installation much easier.

Installing PostgreSQL on MacOS

The terminal will help you create a series of installation operations and create folders to your local machine to accommodate Home-brews storage requirements. First you would have to install PostgreSQL within your terminal by inputting this in your root of your terminal

brew updatebrew install postresql

Next we want to start the Postgres server and to do that we add in these commands below. The first one is to start the server and the second one is to stop the server. In order for your application in rails work, you must have your server running,

pg_ctl -D /usr/local/var/postgres startpg_ctl -D /usr/local/var/postgres stop

Creating Rails Project with PostreSQL

When creating a new rails project with the

rails new <project_name>

command, the default database system will be sqlite3. To create a new rails project with PostgreSQL databases system, you have to tell your application what database you want to use with this command.

rails new <project_name> --database=postgresql -T

This will automatically generate your new rails application and set up to using Postgres. The -T flag is if you don’t want to be running any tests on your application. This flag will not include it within your project. An example on how the first lines should look within your database.yml file

Once your application is set up, as said previously make sure you have your Postgres server running, if not, you will get an error that your Postgres server is not found. Once you open up your rails project, by using Postgres, within your project root directory, you have to run

rails db:create

This is what Postgres requires you to initiate in order to get your development file in your db directory.

For any more information on how to set up your server click here

I am a graduate from Flatiron School for Software Engineer. Studied HTML, CSS, Ruby, Ruby on Rails, JavaScript, React, Redux, Sinatra, and Sqlite3