试图从SQLite切换到PostgreSQL

时间:2015-05-13 20:24:48

标签: ruby-on-rails database sqlite postgresql heroku

我在开发和测试中使用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中,我添加了usernamepassword
  • 在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的连接失败?

1 个答案:

答案 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,所以您可以保留生产部分