【问题标题】:PG::ConnectionBad Rails and PostgreSQL on development MacOSPG::ConnectionBad Rails 和 PostgreSQL 在开发 MacOS 上
【发布时间】:2017-08-19 07:04:42
【问题描述】:

我在 SQlite 和 MySQL 中使用过 Rails。第一次使用 PostgreSQL。刚刚在我的本地机器上安装了 PostgreSQL。使用 Heroku 进行生产。

每次我运行 rake db:migraterake db:create 我都会收到以下错误:

PG::ConnectionBad: fe_sendauth: no password supplied

我使用 Homebrew 安装了 PostgreSQL,但从未设置用户或密码。 这是我的 Postgres 设置:

Daves-MBP-2:project dave$ ps aux | grep postgres 
postgres           670   0.0  0.0  2466764   1816   ??  S    Fri10AM   0:00.33 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdflagwriter
postgres           658   0.0  0.0  2514516   3032   ??  S    Fri10AM   0:01.31 /usr/sbin/distnoted agent
postgres           657   0.0  0.0  2542724   6336   ??  S    Fri10AM   0:05.69 /usr/libexec/trustd --agent
postgres           656   0.0  0.0  2545304   6676   ??  S    Fri10AM   0:04.28 /usr/libexec/lsd
postgres           655   0.0  0.0  2515008   2596   ??  S    Fri10AM   0:00.07 /usr/sbin/cfprefsd agent
postgres           240   0.0  0.0  2508940    920   ??  Ss   Fri10AM   0:01.06 postgres: stats collector process    
postgres           239   0.0  0.0  2653940   1860   ??  Ss   Fri10AM   0:00.64 postgres: autovacuum launcher process    
postgres           238   0.0  0.0  2653940    832   ??  Ss   Fri10AM   0:01.00 postgres: wal writer process    
postgres           237   0.0  0.0  2653940    900   ??  Ss   Fri10AM   0:01.02 postgres: writer process    
postgres           236   0.0  0.0  2653940    896   ??  Ss   Fri10AM   0:00.04 postgres: checkpointer process    
postgres           233   0.0  0.0  2508940    728   ??  Ss   Fri10AM   0:00.00 postgres: logger process    
postgres           108   0.0  0.0  2654196   3372   ??  Ss   Fri10AM   0:00.32 /Library/PostgreSQL/9.6/bin/postmaster -D/Library/PostgreSQL/9.6/data
dave             68244   0.0  0.0  2442020   2012 s000  S+    4:28PM   0:00.00 grep postgres

这是我的 database.yml 文件

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: "mydatabase"


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  adapter: postgresql
  encoding: unicode
  url: "HEROKU URL"

这是我从 pg_hba.conf 中获得的设置 - cd /usr/local/var/postgres/ - 打开 pg_hba.conf

我错过了什么?

谢谢

编辑

以下是来自以下 cmets 的内容:

第一个文件“com.edb.launchd.postgresql-9.6.plist”:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
        <key>Label</key>
        <string>com.edb.launchd.postgresql-9.6</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Library/PostgreSQL/9.6/bin/postmaster</string>
                <string>-D/Library/PostgreSQL/9.6/data</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    <key>UserName</key>
    <string>postgres</string>
    <key>KeepAlive</key>
    <dict>
         <key>SuccessfulExit</key>
         <false/>
    </dict>
</dict>
</plist>

第二个文件“homebrew.mxcl.postgresql.plist”:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.postgresql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/postgresql/bin/postgres</string>
    <string>-D</string>
    <string>/usr/local/var/postgres</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/log/postgres.log</string>
</dict>
</plist>

【问题讨论】:

    标签: ruby-on-rails ruby postgresql heroku


    【解决方案1】:

    在编辑器中打开文件/Library/PostgreSQL/9.6/data/pg_hba.conf。确保 USER 和 METHOD 的值与以下示例匹配(alltrust)。

    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    
    # "local" is for Unix domain socket connections only
    local   all             all                                     trust
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            trust
    # IPv6 local connections:
    host    all             all             ::1/128                 trust
    

    保存文件,然后从终端运行pg_ctl reload -D /Library/PostgreSQL/9.6/data

    【讨论】:

    • 我验证了 pg_hba.conf 有这些设置。当我运行命令重新加载时,我收到此错误:“pg_ctl:无法打开 PID 文件”/Library/PostgreSQL/9.6/data/postmaster.pid“:权限被拒绝”
    • 看起来 Postgres 是由您日志中的另一个用户启动的。通过 Homebrew 安装 Postgres 后,你是如何启动它的?
    • 从我的项目文件夹中:“brew services start postgresql”
    • 似乎 brew 正在一个名为“postgres”的用户下启动服务,这不是您登录的用户。尝试将“用户名:postgres”添加到正在开发的应用程序的 database.yml 文件中。如果您仍然遇到同样的错误,请告诉我。
    • 是的,这行得通...谢谢。 (我还需要密码)。如何更改 Brew 启动它的默认用户?
    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 2017-12-19
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多