【问题标题】:Cloud9 postgresCloud9 postgres
【发布时间】:2014-12-20 03:55:12
【问题描述】:

我正在尝试在 Cloud9 的 Rails 应用程序中设置 postgres 数据库。

我已按照此处的说明操作:https://docs.c9.io/setting_up_postgresql.html 并设置了一个名为 cc_database 的数据库。

我的 database.yml 文件如下所示:

development:
  adapter: postgresql
  encoding: SQL_ASCII
  database: cc_database
  pool: 5
  username: postgres
  password: password

当我运行 rake db:setup 时出现以下错误:

 PG::ConnectionBad: FATAL:  Peer authentication failed for user "postgres"

我对这一切都很陌生,所以任何建议都将不胜感激。

【问题讨论】:

  • 你可以访问你的 c9 服务器的 shell 吗?
  • 并尝试为您的生产环境添加一个块到您的database.yml

标签: ruby-on-rails postgresql cloud9-ide


【解决方案1】:

找到了解决方案。需要编辑 pg_hba.conf 文件以将身份验证从 peer 更改为 md5,如下所示:

local    postgres     postgres     md5

文件很难找到,因为它只能通过cloud9中的终端访问。您在文件树中找不到它。

如果你在 postgres 中输入以下内容,它会显示你的位置

SHOW hba_file;

然后您可以通过终端在 vim 中查找和编辑。

【讨论】:

    【解决方案2】:

    执行以下步骤:

    1. 为 cloud9 上的 postgresql 创建一个新的用户名和密码:

      $ sudo service postgresql start
      $ sudo sudo -u postgres psql
      postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
      postgres=# \q
      
    2. 在 cloud9 上创建 ENV 变量:

      $ echo "export USERNAME=username" >> ~/.profile
      $ echo "export PASSWORD=password" >> ~/.profile
      $ source ~/.profile
      

      我在 cloud9 上用于 rails 4.2.0 的 database.yml:

      default: &default
        adapter: postgresql
        encoding: unicode
        pool: 5
        username: <%= ENV['USERNAME'] %>
        password: <%= ENV['PASSWORD'] %>
        host:     <%= ENV['IP'] %>
      
      development:
        <<: *default
        database: sample_app_development
      
      test:
        <<: *default
        database: sample_app_test
      
      production:
        <<: *default
        database: sample_app_production
      
    3. 在 Gemfile 中包含 gem pg 并安装:

      gem 'pg', '~> 0.18.2'

      $ bundle install
      
    4. 为 cloud9 上的 database.yml 更新 template1 postgresql:

      postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
      postgres=# DROP DATABASE template1;
      postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
      postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
      postgres=# \c template1
      postgres=# VACUUM FREEZE;
      postgres=# \q
      
    5. 从命令行运行:

      bundle exec rake db:create
      

    【讨论】:

    • 在 2015 年 9 月尝试让 thinkbot 吊带与 postgres 和 c9.io 一起工作 - 我可以确认解决方案有效
    • 谢谢你——如果没有你的一步一步,我不知道我是怎么想出来的
    【解决方案3】:

    cloud9 中的 postgresql 设置为在 localhost 连接时与对等方进行身份验证。 因此,快速解决方法是将您的 database.yaml 中的用户更改为当前用户。在我的情况下,用户的名称是 ubuntu。 要查看当前用户,请使用命令

    $ echo $USER

    所以终端中的命令列表是。

    $ sudo su - postgres
    $ createuser ubuntu -dslP
    $ Enter password for new role: **same password from your yaml file**
    $ Enter it again:
    

    像这样设置你的 yaml 文件

    development:
      adapter: postgresql
      encoding: SQL_ASCII
      database: cc_database
      pool: 5
      username: ubuntu
      password: password
    

    现在你可以运行了

    rake db:create
    rake db:migrate
    

    【讨论】:

    • @carlos,这个解决方案对我有用。顺便说一句,你知道如何使用用户ubuntu这种方式登录psql吗?
    • @Matthias,我认为psql -h localhost -U ubuntu -W
    【解决方案4】:

    使用用户名“ubuntu”和空白密码。我的 database.yml 看起来像这样:

    开发:
    适配器:postgresql
    编码:unicode
    数据库:myflix_development
    游泳池:5
    用户名:ubuntu
    密码:

    测试:
    适配器:postgresql
    编码:unicode
    数据库:myflix_test
    游泳池:5
    用户名:ubuntu
    密码:

    如果它随后抱怨密码错误,请尝试使用 Cloud9 的说明更改密码:

    在命令行中输入:sudo sudo -u postgres psql

    postgres=#\password
    输入新密码:leave it blank and press enter
    再次输入:leave it blank and press enter
    postgres=#\q

    我对此很陌生。希望有效!

    【讨论】:

      【解决方案5】:

      对我来说,仅执行Cloud9 workspace setup with Rails and Postgresql 中的步骤是不够的。 它传递了我的用户,但没有传递密码。 echo $USERNAME 什么都没有。

      解决方案

      $ sudo su - postgres
      $ createuser ubuntu -dslP
      

      然后我这样做了:

      sudo sudo -u postgres psql
      postgres=# \password
      Enter new password: entered a real password
      Enter it again: entered it again
      postgres=# \q
      

      然后我在我的 yaml 文件中执行此操作(注意我杀死了主机部分):

      development:
        adapter: postgresql
        encoding: unicode
        database: my_database_name
        pool: 5
        username: ubuntu
        password: actual_password
      

      然后我可以创建我的数据库:

      rake db:create
      

      我的 Rails 服务器启动时没有任何问题。

      【讨论】:

      • 应该是echo $USER 而不是echo $USERNAME
      【解决方案6】:

      如何在 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 部分中的templateusernamepassword 配置是必不可少的)。

      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

      【讨论】:

      • 尝试使用 Cloud 9 在另一台计算机上处​​理项目时,我感到非常沮丧。尝试了很多事情来使事情正常运行……这成功了!谢谢。
      【解决方案7】:

      在创建任何数据库之前拥有 rails 应用程序的简短版本:

      添加 gem 'pg' 删除 gem sqlite3

      $ bundle install
      $ gem install pg
      

      那么,

      $sudo service postgresql start
      $psql -c "create database myapp_development owner=ubuntu"
      

      根据https://community.c9.io/t/how-do-i-set-up-postgresql-on-c9-for-my-rails-app/2614/4

      将 myapp 更改为任何名称。然后,在下面复制粘贴,将 myapp 更改为任何名称。

      /myapp/config/database.yml
          development:
            adapter: postgresql
            encoding: unicode
            database: myapp_development
            pool: 5
            username: ubuntu
            password:
            timeout: 5000
      

      和上面的cmets一样,不需要密码,用户名可以留在ubuntu。

      $rake db:migrate
      

      如果您使用的是 heroku,则不需要 database.yml 中的生产部分

      【讨论】:

        猜你喜欢
        • 2012-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-30
        • 2014-11-29
        • 2019-07-19
        • 2018-06-21
        • 1970-01-01
        相关资源
        最近更新 更多