我在开发和测试中使用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
然后:
gem 'pg'
仅从生产环境移至所有环境并移除gem 'sqlite3'
bundle install
。sudo service postgresql start
sudo sudo -u postgres psql
create database "app_development";
\q
更新:我添加了以下附加步骤:
CREATE USER my_username SUPERUSER PASSWORD 'my_password';
username
和password
host: myurl.c9.io
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的连接失败?
答案 0 :(得分:1)
将database.yml文件的内容替换为:
default: &default
adapter: postgresql
host: localhost
username: yourusername
password: yourpassword
timeout: 5000
port: 5432
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
因为您使用Heroku,所以您可以保留生产部分