go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Database Setup for PostgreSQL
 
Subject: Database Setup for PostgreSQL
Author: Linux
In response to: Step 5 Create a model
Posted on: 09/13/2017 01:23:49 AM

Ruby on Rails recommends to create three databases: one for test, one for development and one for production environment

Check Install PostgreSQL if you do not have the PostgreSQL ready.

First, start the db server:

administrator@ubuntu:~$ sudo /etc/init.d/postgresql restart


Secondly, create a service account blog_admin:
administrator@ubuntu:~$ sudo -u postgres createuser blog_admin --no-createdb --no-superuser --no-createrole --pwprompt
Enter password for new role: <secret>
Enter it again: <secret>


Thirdly, create three databases: blog_test, blog_development and blog_production
administrator@ubuntu:~$ sudo -u postgres createdb blog_test --owner=blog_admin
administrator@ubuntu:~$ sudo -u postgres createdb blog_development --owner=blog_admin
administrator@ubuntu:~$ sudo -u postgres createdb blog_production --owner=blog_admin


Finally, you need to let Rails know about the user name and password for the databases. You do this in the file /config/database.yml within your Rails project directory.

When you finish, it should look something like :
# PostgreSQL

default: &default
   adapter: postgresql
   encoding: unicode
   username: blog_admin
   password: secret
   host: localhost
  
development:
   <<: *default
   database: blog_development

production:
   <<: *default
   database: blog_production

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
   <<: *default
   database: blog_test



 

> On 09/13/2017 01:21:04 AM Linux wrote:


To create a new model, run this command:
administrator@ubuntu:~/blog$ rails generate model Post title:string text:text

Running via Spring preloader in process 44241
      invoke  active_record
      create    db/migrate/20170912222917_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/models/post_test.rb
      create      test/fixtures/posts.yml


This is going to do:
  • Create a model Post inside file app/models/post.rb
    class Post < ApplicationRecord
    end
    


  • Generate a database instruction file db/migrate/20170912222917_create_posts.rb
    class CreatePosts < ActiveRecord::Migration[5.1]
      def change
        create_table :posts do |t|
          t.string :title
          t.text :text
    
          t.timestamps
        end
      end
    end
    

    to create a table posts with a title column of type string and a text column of type text, together with timestamps

  • Map the model Psot with the database table posts





    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.