Note: Open (PG)PostGreSQL in command line and do some stuffs

To access postgres console:

$ sudo -u postgres -i

postgres@host:~$ psql

or

$ sudo -u postgres psql

[sudo] password for john: 
psql (9.3.12)
Type "help" for help.

postgres=# 

 

# For help
postgres=# help
 You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
 \h for help with SQL commands
 \? for help with psql commands
 \g or terminate with semicolon to execute query
 \q to quit
# For help
postgres=# \h
 Available help:
 ABORT CLUSTER DEALLOCATE END
 ALTER AGGREGATE COMMENT DECLARE EXECUTE
 ALTER COLLATION COMMIT DELETE EXPLAIN

Create database

# database command has to end with semicolon
postgres=# create database july_prod_dump1;
CREATE DATABASE

Alter permission

ALTER USER new_user CREATEDB

 

Passing Modifiers :: rails generate migration

2.3 Passing Modifiers

Some commonly used type modifiers can be passed directly on the command line. They are enclosed by curly braces and follow the field type:

For instance, running:

$ bin/rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}

will produce a migration that looks like this

class AddDetailsToProducts < ActiveRecord::Migration
  def change
    add_column :products, :price, :decimal, precision: 5, scale: 2
    add_reference :products, :supplier, polymorphic: true, index: true
  end
end

Have a look at the generators help output for further details.

Advanced usage

Sometimes you have to automatically add index for columns in your migration. It’s not a problem:

$ rails g model user email:index location_id:integer:index

Or uniq index:

$ rails g model user pseudo:string:uniq

Set limit for field of integer, string, text and binary fields:

$ rails generate model user pseudo:string{30}

Special syntax to generate decimal field with scale and precision:

$ rails generate model product 'price:decimal{10,2}'

Pay attention that you have to wrap parameter price:decimal{10,2} to quotes. It’s vital and you may have incorrect behavior of generator if you don’t do it. Full explanation of this case is here.

You can combine any single curly brace option with the index options:

$ rails generate model user username:string{30}:uniq

And the last useful feature of generators – it’s options to generate reference columns (fields which are used in rails as foreign keys):

$ rails generate model photo album:references

This command will generate photos table with integer field album_id and also it will add index for this field automatically. Make sure in it by looking at generated migration:

class CreatePhotos < ActiveRecord::Migration
  def change
    create_table :photos do |t|
      t.references :album

      t.timestamps
    end
    add_index :photos, :album_id
  end
end

For polymorphic reference use this syntax:

$ rails generate model product supplier:references{polymorphic}

Polymorphic reference with indexes:

$ rails generate model product supplier:references{polymorphic}:index

 Sources

http://railsguides.net/advanced-rails-model-generators/