Subject: Create Table for Model
Author: Linux
In response to: Database Setup for PostgreSQL
Posted on: 09/13/2017 01:26:07 AM
Now, you can use a Rails command to run the migration:
administrator@ubuntu:~/blog$ rails db:migrate
== 20170912222917 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0216s
== 20170912222917 CreatePosts: migrated (0.0219s) =============================
administrator@ubuntu:~/blog$ rails db:migrate RAILS_ENV=test
== 20170912222917 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0057s
== 20170912222917 CreatePosts: migrated (0.0060s) =============================
administrator@ubuntu:~/blog$ rails db:migrate RAILS_ENV=production
== 20170912222917 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0056s
== 20170912222917 CreatePosts: migrated (0.0059s) =============================
You can verify the table by connecting to the database:
administrator@ubuntu:~/blog$ psql -h localhost -p 5432 -U blog_admin blog_development
Password for user blog_admin:
psql (9.6.5, server 9.3.19)
SSL connection (protocol: TLSv1.2, cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
blog_development=> \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+------------
public | ar_internal_metadata | table | blog_admin
public | posts | table | blog_admin
public | schema_migrations | table | blog_admin
(3 rows)
blog_development=> \q
>
> On 09/13/2017 01:23:49 AM
Linux wrote:
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
References: