如何在 Cloud9 上设置 PostgreSQL 和 Rails
在撰写本文时,Cloud9 已预安装 PostgreSQL,因此您无需自行安装。但是,它默认不运行,因此您需要在终端中使用以下命令启动它:
sudo service postgresql start
将 PostgreSQL 密码更改为“密码”(或选择其他密码):
sudo sudo -u postgres psql
# This will open the psql client.
# Type \password and press enter to begin process
# of changing the password:
postgres=# \password
# Type your new password (e.g. "password") and press enter twice:
Enter new password:
Enter it again:
# Password changed, quit psql with \q
postgres=# \q
将您的config/database.yml 编辑为:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
# Important configs for cloud9, change password value
# to what you entered in the previous psql step.
template: template0
username: ubuntu
password: password
development:
<<: *default
database: your_app_name_development
test:
<<: *default
database: your_app_name_test
production:
<<: *default
database: your_app_name_production
username: your_app_name
password: <%= ENV['YOUR_APP_NAME_DATABASE_PASSWORD'] %>
(请注意上面default 部分中的template、username 和password 配置是必不可少的)。
将pg gem 添加到您的Gemfile:
gem 'pg'
运行bundle install。
从您的 Gemfile 中删除 sqlite gem(并可选择删除 db/*.sqlite3 文件)。
使用db:setup 任务创建数据库、加载 schema.rb 并为数据库播种:
bundle exec rake db:setup
# Run bin/rake -AD db to see all db-related tasks
启动或重新启动您的 Rails 应用并检查它是否正常工作。
请注意,旧 sqlite 数据库中的非种子数据不会出现在新数据库中。
如果您想使用 psql 客户端直接与 PostgreSQL 交互,请在终端中运行 psql 或运行 bin/rails db。