【发布时间】:2015-05-13 20:24:47
【问题描述】:
我在开发和测试中使用 SQLite,在 Heroku 上的生产中使用 PostgreSQL。我想用 PostgreSQL 替换 SQLite。我在 Cloud9 环境(Rails 4)中编程。我没有可能丢失的数据。
我做了什么:
首先,我编辑了database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
然后:
- 在 Gemfile 中,我仅将
gem 'pg'从生产环境移动到所有环境并删除了gem 'sqlite3' - 我跑了
bundle install。 - 我跑了
sudo service postgresql start - 我跑了
sudo sudo -u postgres psql - 并输入
create database "app_development"; - 输入
\q
更新:我添加了以下附加步骤:
- 我在 psql 中使用
CREATE USER my_username SUPERUSER PASSWORD 'my_password';创建了一个新用户 - 在 database.yml 我添加了
username和password - 在 database.yml 我添加了
host: myurl.c9.io - 我在psql中输入:
GRANT ALL ON DATABASE app_development to my_username
运行sudo sudo -u postgres psql 然后\list 产生:
Name | Owner | Encoding | Collate | Ctype | Access privileges
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
app_development | postgres | SQL_ASCII | C | C |
我在这里没有看到我的用户名是 app_development 的所有者...
错误:运行rake db:migrate超时,声明PG::ConnectionBad: could not connect to server: Connection timed out Is the server running on host "myurl.c9.io" (ip address) and accepting TCP/IP connections on port 5432?。
为什么与 PostgreSQL 的连接会失败?
【问题讨论】:
-
您没有在 database.yml 中提及用户名/密码 ....
-
我应该如何设置密码?我输入了
sudo sudo -u postgres psql,然后输入了\password,然后输入了我的新密码。在 database.yml 中,我知道我应该包含username: <%= ENV['USERNAME'] %>、password: <%= ENV['PASSWORD'] %>和host: <%= ENV['IP'] %>。然后我应该在哪个文件中实际输入我的密码(也许在 config\secrets.yml 中?)?另外两个变量应该是什么?
标签: ruby-on-rails database sqlite postgresql heroku