Rails With Postgresql database, complete example
TAGS:
ruby rails postgres database
1- Creating Postgresql Database.
First, log into postgres-server
sudo su postgres -c psqlType password and be ready to use postgresql DSL. First, you may need to determine that you will create more than one database, one for development environment, and one for test, and the third for production environment.
Here, We will create one for development only, and you can create the others.
create database rails_dev;Type password and be ready to use postgres DSL. First, Create you may need to determine that you will create more than one database, one for development environment, and one for test, and the third for production environment.
Here, We will create one for development only, and you can create the others.
create database rails_dev;Also, You need to create user:
create user osama with password '123456789';Then, Grant all privileges to this user
grant all privileges on database rails_dev to osama;At this point, you have database name, username and password.
2- Creating Rails Application With Postgresql database.
rails new auto --database=postgresql
cd auto
subl .The default database for rails is sqlite3, but we use --database=postgresql to use postgres instead, so you will find ` gem ‘pg’.
Note, If you change from sqlite3 to postgres, you need to make
bundle installto update gemfile.lock and install pg gem if not installed.
Open config/database.yml and fill
default: &default
adapter: postgresql
encoding: unicode
port: 5432
username: osama
password: 123456789
pool: 5
development:
<<: *default
database: rails_devNow, Create you posts using scaffold :
rails g scaffold post name comment:textAnd then, Make migration to create the posts table in the database it self.
rake db:migrateLast, Fire up you rails server:
rails sNow, ready to browse to localhost:3000/posts and process all CRUD operations.
All these steps in this tutorial video :