【问题标题】:Migrating from SQLite to Postgres for deployment on Heroku. PG::ConnectionBad. Using Cloud9从 SQLite 迁移到 Postgres 以在 Heroku 上进行部署。 PG::ConnectionBad。使用 Cloud9
【发布时间】:2017-01-26 21:29:05
【问题描述】:

我正在尝试从 SQLite 迁移到 Postgres,以便应用在 Heroku 上正确部署。但我没有任何运气。

rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

我是 Postgres 的新手,但我尝试了所有遇到的建议,但都没有运气。这是我的 database.yaml 文件:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  host: localhost
  username: postgres 
  password: postgres

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production

我相信我正确设置了 Postgres 列表中的空数据库,但运行 rake db:migrate 失败。

非常感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    如果你还没有运行:

    sudo service postgresql start
    

    在cloud9终端中

    然后切换到您的 postgres 用户并验证您是否创建了您在 database.yaml 中指定的数据库

    sudo su - postgres
    psql
    

    然后在 psql 提示符下输入以下命令列出所有可用的数据库

    \list
    

    如果你需要创建一个数据库,你可以通过键入来完成

    CREATE DATABASE "database_name";
    

    为 postgres 用户设置密码

    \password postgres
    

    完成上述步骤后,确保您的 database.yaml 文件具有正确的数据库名称、用户名和密码并尝试运行

    rake db:migrate
    

    如果数据库设置正确并配置了 database.yaml 文件

    尝试重启 postgres 服务器

    sudo /etc/init.d/postgresql restart
    

    然后再次运行rake db:migrate

    来源

    Cloud9 docs 设置 PostgresSQL 相关

    Cloud9 Forum Post 与在 Rails 应用程序中设置 PostgreSQL 相关

    Stackoverflow post 与更改 psql 密码相关

    Stackoverflow post 重启 PostgreSQL 服务器相关

    替代方案 我设法将我已经开始使用 SQLite 开发的应用程序推送到 heroku 的另一种方法是将 SQLite gem 移动到我的 Gemfile 中的开发组中,并添加一个具有 heroku 依赖项的生产组:

    group :production do
      gem 'rails12_factor'
      gem 'pg'
    end
    

    然后在没有生产组的情况下在开发包中安装:

    bundle install --without production
    

    当我推送到 heroku 时进行正常的捆绑安装

    【讨论】:

    • 还是没有运气。是否有可能 template0 或 template1 已损坏?有没有办法重置一切并从头开始?我已经盯着同样的错误消息看了 3 天了。
    • 我不完全确定,但是我在另一个关于 Clobbered default database in PostgreSQL 的线程中发现了一些关于它的更多信息,这似乎表明 template0 受到保护
    • @Jason 该线程建议使用pg_dropcluster 删除集群并重新安装,而不是执行完全清除How to thoroughly purge and reinstall postgresql on ubuntu 我相信Cloud9 开发环境应该使用Ubuntu。
    • @Jason 您还应该尝试重新启动 PostgreSQL 服务器。我编辑了我的回复以包含说明。今天登录到我自己的 cloud9 环境后,我收到了与您相同的错误并重新启动修复它。
    猜你喜欢
    • 2020-08-30
    • 2015-08-10
    • 2014-07-21
    • 1970-01-01
    • 2014-10-27
    • 2015-09-11
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多