Guest User

Untitled

a guest
Mar 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. ## `database.yml` file
  2. * Every Rails app will interact with a database
  3. * You can connect a database by setting an environment variable `ENV['DATABASE_URL']`
  4. * **ALTERNATIVE**: Using a configuration file in `config/database.yml`
  5.  
  6. ```yml
  7. development:
  8. adapter: postgresql
  9. database: blog_development
  10. pool: 5
  11. ```
  12.  
  13. * This will connect to a database named `blog_development` using the `postgresql` adapter
  14. * This file contains information for 3 types of environments:
  15. * `development`: environment used on your local computer as you code and interact with the
  16. database
  17. * `test`: used when running automated test
  18. * `production`: used when your app is deployed for the world to use.
  19. * You can specify a URL to connect to a DB:
  20.  
  21. ```
  22. development:
  23. url: postgresql://localhost/blog_development?pool=5
  24.  
  25. ```
  26.  
  27. * May contain `erb` tags
  28. * Default DB is SQLite in Rails, to set up another DB upon new Rails app
  29. `rails new demo_app database=mysql` to use a MYSQL DB.
  30.  
  31. ---
  32.  
  33. ## SQLite DB
  34. * SQLite is a lightweight **serverless** database application, best suited for **development** and
  35. **testing**
  36. * simple configuration for the `database.yml` file:
  37.  
  38. ```yml
  39. development:
  40. adapter: sqlite3
  41. database: db/development.sqlite3
  42. pool: 5
  43. timeout: 5000
  44. ```
  45.  
  46. ## PSQL DB
  47. * In terminal, enter: `sudo -u postgres creatuser rubyuser -s`
  48. * `rubyuser` is the name of the user, can be any name
  49. * To create a password for this user:
  50.  
  51. ```
  52. sudo -u postgres psql
  53. postgres=# \password rubyuser
  54. ```
  55.  
  56. * To create a table for the different environments:
  57.  
  58. ```
  59. postgres=# CREATE DATABASE name_of_database OWNER rubyuser;
  60.  
  61. CREATE DATABASE
  62. ```
  63.  
  64. * Proper name of DB should be `name_development`, `name_test`, `name_production`
  65. * ** Pressing `CTRL-D` will terminate postgres **
  66.  
  67. ### Configuring the `database.yml` file
  68. * Need to tell ruby what the password and username is for each of the database tables created
  69.  
  70. ```
  71. default: &default
  72. adapter: postgresql
  73. encoding: unicode
  74.  
  75. development:
  76. <<: *default
  77. database: name_development
  78. username: rubyuser
  79. password: <password entered in the terminal>
  80.  
  81. test:
  82. <<: *default
  83. database: name_test
  84. username: rubyuser
  85. password: <..>
  86.  
  87. production:
  88. <<: *default
  89. database: name_production
  90. username: rubyuser
  91. password: <..>
  92. ```
  93.  
  94. * For each table created above you should use a different name and a different password
Add Comment
Please, Sign In to add comment