每次我运行rails 4.0服务器时,都会得到这样的输出。

Started GET "/" for 127.0.0.1 at 2013-11-06 23:56:36 -0500

PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
:
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize'
 activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
 activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
 activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection'
 activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection'
 activerecord (4.0.0) lib/active_record/migration.rb:792:in `current_version'
 activerecord (4.0.0) lib/active_record/migration.rb:800:in `needs_migration?'
 activerecord (4.0.0) lib/active_record/migration.rb:379:in `check_pending!'
 activerecord (4.0.0) lib/active_record/migration.rb:366:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1613334440513032208__call__callbacks'
 activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
 actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
 better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
 railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
 activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
 railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
 quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets'
 actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
 rack (1.5.2) lib/rack/runtime.rb:17:in `call'
 activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
 rack (1.5.2) lib/rack/lock.rb:17:in `call'
 actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
 railties (4.0.0) lib/rails/engine.rb:511:in `call'
 railties (4.0.0) lib/rails/application.rb:97:in `call'
 rack (1.5.2) lib/rack/content_length.rb:14:in `call'
 thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
 thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
 thin (1.5.1) lib/thin/connection.rb:54:in `process'
 thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
 eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
 thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
 thin (1.5.1) lib/thin/server.rb:159:in `start'
 rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
 rack (1.5.2) lib/rack/server.rb:264:in `start'
 railties (4.0.0) lib/rails/commands/server.rb:84:in `start'
 railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>'
 railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
 bin/rails:4:in `<main>'

我运行的是Mavericks OS X 10.9,所以我不知道这是否是问题所在。我已经尽力了,但似乎都没用。我已经卸载和安装postgres和pg gem多次了。

这是我的数据库。yml文件

development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: 
  password: 
  template: template0
  host: localhost
  port: 5432

test: &test
  adapter: postgresql
  encoding: unicode
  database: metals-directory_test
  pool: 5
  username: 
  password: 
  template: template0
  host: localhost
  port: 5432

staging:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_production
  pool: 5
  username:
  password:
  template: template0
  host: localhost

production:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_production
  pool: 5
  username:
  password:
  template: template0
  host: localhost

cucumber:
  <<: *test

你的系统中安装了postgresql吗?如果没有,请观看安装postgresql。在你成功地将postgresql集成到你的系统中之后,你可以在你的系统终端中输入如下内容:

which psql
#=> /usr/bin/psql

之后,你需要在postgresql中创建一个用户和数据库,如下所示:

sudo su - postgres
psql

然后,您可以在终端中看到以下内容

postgres=#

类型:

CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production  WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;

完成此操作后,您需要更正database.yml。也许你需要这样的东西:

development:
  adapter: postgresql
  encoding: unicode
  database: metals-directory_development
  pool: 5
  username: yourname
  password: passwordhere   ### password you have specified within psql
  host: localhost
  port: 5432               ### you can configure it in file postgresql.conf

另外,如果你在postgresql上有问题,检查pg_hba.conf也是个好主意

它可能只是一个陈旧的PID文件。它可能会无声地失败,因为你的计算机没有完全完成关机过程,这意味着postgres没有删除PID(进程id)文件。

PID文件被postgres用来确保在同一时间只有一个服务器实例在运行。所以当它再次启动时,它会失败,因为已经有一个PID文件告诉postgres服务器的另一个实例已经启动(即使它没有运行,它只是没有关闭和删除PID)。

要修复它,请删除/重命名PID文件。找到postgres数据目录。在macOS上使用homebrew,它在/usr/local/var/postgres/, 或者/usr/local/var/log/其他系统可能是/usr/var/postgres/。在M1上,它可能是/opt/homebrew/var/postgresql。 要确定这是问题所在,请查看日志文件(server.log)。在最后一行你会看到:

致命:锁定文件"postmaster. "Pid "已经存在 提示:在数据目录“/usr/local/var/postgres”中是否运行另一个postmaster (PID 347) ?

如果是,rm postmaster.pid 重新启动服务器。在使用launchctl(带homebrew)的mac上,以下命令将重新启动服务器。 Brew服务重启postgresql

或者在旧版本的Brew上

    launchctl unload homebrew.mxcl.postgresql.plist  
    launchctl load -w homebrew.mxcl.postgresql.plist

检查postgresql.conf文件(在ubuntu中是在/etc/postgresql/X.X/main/postgresql.conf文件中),寻找如下一行:

listen_addresses="localhost"

试着把它改成:

listen_addresses="*"

它将接受每一个IP,接下来检查一行说:

port=5432

并检查是否与数据库的端口相同。Yml,默认使用5433代替5432,不要忘记重启postgres服务器,

好运!

我也有同样的问题,这个解释帮我解决了:http://blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/

关键步骤是查看/usr/local/var/ postgress /server.log的尾部, 这让我看到了真正的问题是什么,那就是我还没有完全完成升级PostgreSQL的过程

克里斯·斯莱德的回答帮助了我。

我写了一个小脚本来杀死那些剩余的进程,如果有用的话:

kill_postgres() {
  if [[ $* -eq "" ]]; then
    echo "Usage: 'kill_postgres <db_name>' to kill remaining instances (Eg. 'kill_postgres my_app_development')"
  else
    gksudo echo "Granted sudo"
    pids="$(ps xa | grep postgres | grep $* | awk '{print $1}' | xargs)"
    if [[ $pids -eq "" ]]; then
      echo "Nothing to kill"
    else
      for pid in "${pids[@]}"
      do
        echo "Killing ${pid}"
        sudo kill $pid
        echo "Killed ${pid}"
      done
      kill_postgres $*
    fi
  fi
}

我已经设法通过Chris Slade的回答来解决这个问题,但要重新启动服务器,我必须使用以下命令:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

我在这里找到的答案(pjammer的答案在底部)

要用Postgres解决这类问题,并在Mac OSX上使用Postgres,这可能是我迄今为止发现的最好和最简单的解决方案:

http://postgresapp.com/

下载、安装,你就会开心了:)

将主机:localhost放入数据库。执行以下命令:

rake db:create db:migrate  

正如上面所建议的,我只是在我的Mac上打开Postgres应用程序,点击打开Psql,关闭Psql窗口,在我的终端上重新启动我的rails服务器,它又开始工作了,没有更多的错误。

相信大象:http://postgresapp.com/

如果你在将postgres升级到一个新的主版本(f.x 9.3.0到9.4.0或更高版本)后遇到这个问题,那么这样做:

@dmitrygusev's fix from https://github.com/Homebrew/homebrew/issues/35240 Following official [Postgresql] migration guide helped: brew switch postgres 9.3.5 # presuming you already installed 9.4.1 pg_dumpall > outputfile launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist mv /usr/local/var/postgres /usr/local/var/postgres.old brew switch postgres 9.4.1 initdb -D /usr/local/var/postgres psql -d postgres -f outputfile That's all. Check if import went well, then delete backups: rm outputfile rm -Rf /usr/local/var/postgres.old

这里的问题是,在postgres的主要版本升级中,有必要重新创建/迁移数据库。并可能chown目录或手动调用initdb。

请参见:如何在不丢失数据的情况下将PostgreSQL从9.6版本升级到10.1版本?


如果你不使用Homebrew,其他一些提示可能会派上用场:

手动停止PG服务器:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop

手动启动PG服务器:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

我的问题出在申请上。yml文件。我在heroku上的数据库url没有使用5342端口。检查你的heroku配置var DATABASE_URL。确保它与应用程序中的完全匹配。Yml为适用的数据库。

我刚刚运行这个命令sudo service postgresql restart 一切都恢复正常了。

卸载pg: Gem卸载pg 卸载postgres: 酿造卸载postgres 核化postgres文件夹,它可能会有一堆陈旧的东西在里面徘徊: Rm -rf /usr/local/var/postgres 重新启动(可能没有必要) 重新安装pg: 酿造安装postgres 在Chris Slade的回答中,我的评论是从pg开始的,现在我使用brew服务,它在很多方面简化了我的生活: Brew安装服务 从pg开始: Brew服务启动postgresql 重新安装宝石: Gem install pg

和bobsyouruncle。

您需要重新启动Postgresql服务器。

如果你使用的是ubuntu,你可以通过以下命令重新启动Postgresql

sudo service postgresql restart

我在制作过程中遇到了同样的问题(开发过程中一切正常), 在我的情况下,DB服务器不是在同一台机器上的应用程序,所以最后什么工作只是通过编写迁移:

bundle exec rake db:migrate RAILS_ENV=production

然后重新启动服务器,一切正常。

我知道有点晚了,但也许能帮到别人。 我也有同样的问题。结果我有两个版本的postgres 9.1和9.5。我卸载了9.1和9.5,重新安装了9.5,它为我工作。

找到你的postgres文件,它可以在/usr/local/var/postgres/或/usr/var/postgres/,然后删除postmaster。Pid文件在该文件夹中。

我停止了rails服务器,运行rake db:migrate并启动我的rails s。

正如@Magne所描述的,PostgreSQL的主要/次要版本升级(例如9.5 -> 9.6或9 -> 10)后,会出现PG::ConnectionBad - could not connect to server: Connection refused错误。

在postgresql 9.6版本发布后运行brew upgrade postgresql后,我得到了这个错误。问题是主要/次要版本升级需要额外的步骤来将旧日期迁移到新版本。

如何检查这是否是你的问题

你可以检查这是否是问题通过检查最新的酿造公式PostgreSQL版本安装与自制…

$ brew info postgresql

/usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M)
Poured from bottle on 2016-10-14 at 13:33:28
/usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) *
Poured from bottle on 2017-02-06 at 12:41:00

...然后将其与当前PG_VERSION进行比较

$ cat /usr/local/var/postgres/PG_VERSION
9.5

如果PG_VERSION小于最新的酿造公式,并且差异是主要/次要版本更改,那么这可能是您的问题。

如何修复(即如何升级数据)

下面的说明是从9.5升级到9.6。根据您自己的升级更改版本号

步骤1。确保PostgreSQL关闭:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql

步骤2。创建一个新的原始数据库:

$ initdb /usr/local/var/postgres9.6 -E utf8

步骤3。检查新旧二进制版本是什么:

$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1

注意,在这个例子中,我从9.5.4二进制升级到9.6.1二进制

步骤4。使用pg_upgrade实用程序将当前数据迁移到新数据库。

$ pg_upgrade \
  -d /usr/local/var/postgres \
  -D /usr/local/var/postgres9.6 \
  -b /usr/local/Cellar/postgresql/9.5.4/bin/ \
  -B /usr/local/Cellar/postgresql/9.6.1/bin/ \
  -v

-d flag指定当前数据目录 -D flag指定要创建的新数据目录 -b指定旧的二进制文件 -B指定要升级到的新二进制文件

第5步。将旧的数据目录移开

$ mv /usr/local/var/postgres /usr/local/var/postgres9.5

步骤6。将新创建的数据目录移动到PostgreSQL期望的位置

$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres

步骤7。重新启动PostgreSQL

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql

步骤8。如果您正在为Rails使用pg gem,您应该通过卸载和重新安装gem来重新编译(如果您不使用pg gem,请跳过此步骤)。

$ gem uninstall pg
$ gem install pg

步骤9.(可选)在你确信一切正常后,你可以运行以下命令重新获得一些磁盘空间:

brew cleanup postgresql

...如果你真的有勇气,你可以用下面的命令删除旧的PostgreSQL数据目录

rm -rf /usr/local/var/postgres9.5/

(这个答案是基于一篇优秀的博客文章https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/,并进行了一些补充)

我也有同样的问题。I检查PostgreSQL日志文件/var/log/postgresql最后一行。 在/etc/postgresql/9.5/main/postgresql.conf文件中有一个无法识别的配置参数。注释postgresql.conf中的错误行解决了我的问题。

在升级postgresql后,我遇到了这个错误。我从这篇文章中找到了解决问题的方法。我能够让postgres恢复并运行,甚至迁移了所有现有的数据库。 https://coderwall.com/p/ti4amw/how-to-launch-postgresql-after-upgrade

我只是遇到了这个问题,但没有一个建议的解决方案对我有效。在谷歌搜索了很多之后,我确实找到了一个解决方案。这对我来说很管用。

首先,我必须运行这个命令来启动服务器,我猜设置配置文件的位置。

pg_ctl -D /usr/local/var/postgres start && brew services start postgresql

然后我运行这个命令来访问postgres

psql postgres

在postgres提示符下,我输入“\du”来列出角色

postgres=# \du

postgres角色缺失,所以我必须用这个命令创建它

CREATE ROLE POSTGRES WITH SUPERUSER CREATEDB CREATEUSER CREATEROLE REPLICATION BYPASSRLS ;

这解决了我的问题,我希望这能帮助到其他人。

这才是真正帮助我的。

$ cd /usr/local/var/postgres/
$ rm postmaster.pid

参考: http://alumni.lewagon.org/questions/60

使用Postgres应用程序的Mac用户可能希望打开该应用程序(重点搜索Postgres或在菜单栏中找到大象图标)。在其中,你可能会看到一个红色的X,上面写着:“过时的邮政局长。pid文件”。不幸的是,聚光灯搜索不会显示该文件的位置。点击“服务器设置…”,在打开的对话框中,点击“显示”按钮,打开数据目录。浏览其中一个文件夹(对我来说是“var-10”),并删除邮政管理员。pid文件。

回到Postgres应用程序,点击开始按钮。红色的X应该变成带有“运行中”信息的绿色复选标记。现在您应该能够成功地在终端中运行Rails服务器之类的Rails命令了。

你不需要删除邮政局长。Pid文件,因为这可能会导致数据损坏。

选择吗?简单地终止进程(不要使用kill -9,只需一个普通的kill即可)。

然后重新启动postgres服务器,就可以开始了!

以下是实现这一目标的步骤:

找到并打开邮政局长。pid文件(我的是在Mac Sierra) vi ~/Library/Application\ Support/Postgres/var-10/postmaster.pid 复制PID -这是邮政局长的第一行上的数字。pid文件 用Kill PID杀死进程,例如,如果我的PID是381,我就会杀死381 重启Postres -如果使用brew,请执行brew服务启动postgresql。 或者如果使用postgresapp,只需简单地单击开始按钮

我在Osx Movaje上更新我的mac后也遇到了同样的问题。

我找到了这个解决方案:

首先在你的终端试试下面的命令行:

brew services restart postgresql

如果没有变化:

ps aux | grep postgres

如果仍然没有改变:

ls -ls | grep post

最后一个命令来修复它,删除postgres锁文件从根执行:

rm /usr/local/var/postgres/postmaster.pid

然后:

brew services restart postgresql

来自berziiii: https://github.com/ga-wdi-boston/capstone-project/issues/325

希望能有所帮助:)

问候! !

我今晚在开发一个rails应用程序时遇到了这个问题,我已经开发了一段时间。我的问题归结于我的postgresql服务器没有运行。

我转到屏幕顶部(我用的是Mac),点击小大象图标,然后点击“开始”。

我们的服务器没有打开。

希望这能为某些人提供一个简单的解决方案。

Homebrew包管理器包括自动启动的launchctl plists。更多信息请运行brew info postgres。

手动启动:

pg_ctl -D /usr/local/var/postgres start

手动停止:

pg_ctl -D /usr/local/var/postgres stop

自动启动:

让launchd现在启动postgresql并在登录时重新启动:

Brew服务启动postgresql

我不得不重新安装postgres,这里列出了很好的说明: https://medium.com/@zowoodward/effectively-uninstall-and-reinstall-psql-with-homebrew-on-osx-fabbc45c5d9d

然后我必须创建postgres用户: /usr/local/opt/postgres/bin/createuser -s postgres

这种方法将破坏您的所有本地数据,因此如果需要,请备份您的数据。

在我的案例中,存在权限问题。

当我看到日志,我发现了问题,运行

cat /usr/local/var/log/postgres.log 

我发现

2020-07-17 15:08:47.495 PKT [16282] FATAL:  data directory "/usr/local/var/postgres" has invalid permissions
2020-07-17 15:08:47.495 PKT [16282] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

我只是跑了

sudo chmod -R 700 /usr/local/var/postgres

它工作。

我尝试了上面所有的答案,但对我不起作用。

在我的情况下,当我在/usr/local/var/log/postgres.log上检查日志时。这很好,没有错误。但我可以看到它正在监听我的本地IPV6地址,它是“::1”

在我的数据库中。事情是这样的

host:     <%= ENV['POSTGRESQL_ADDON_HOST'] || '127.0.0.1' %>

我把它改成

host:     <%= ENV['POSTGRESQL_ADDON_HOST'] || 'localhost' %>

然后就成功了

对于MacOS,我使用这个:

brew info postgres

而在输出的最末端,我看到:

...
To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

所以我只是使用这个命令pg_ctl -D /usr/local/var/postgres start和psql begin work。

如果移除邮政局长。Pid并不能解决这个问题,这对我来说是有效的:

cd /usr/local/var/
rm -rf postgres/
mkdir postgres
initdb --locale=C -E UTF-8 postgres/
brew services restart postgresql

这要归功于@spirito_libero在这个线程上的解决方案。

对我来说,这很简单:我有一个过时的邮政局长。pid文件。

简单地删除和重新启动postgres为我修复了它:

rm '/Users/<username>/Library/Application Support/Postgres/var-10/postmaster.pid'

这发生在我升级Postgres后(通过Homebrew)。我从12版升级到13版。

如果是这种情况,尝试运行postgres -D /usr/local/var/postgres并检查输出。在我的情况下,它声明数据库文件与服务器不兼容。

您可能会得到一个postgres错误消息。旧的已经存在

Error: /usr/local/var/postgres.old already exists!

要解决这个问题,只需删除该文件

rm -rf /usr/local/var/postgres.old

修复数据库就像运行:

brew postgresql-upgrade-database