Ruby-on-rails PG::ConnectionBad - 无法连接到服务器:连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19828385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PG::ConnectionBad - could not connect to server: Connection refused
提问by fadelakin
Every time I run my rails 4.0 server, I get this output.
每次我运行 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>'
I'm running Mavericks OS X 10.9 so I don't know if that's the problem. I've tried everything I could but nothing seems to work. I've uninstalled and install both postgres and the pg gem multiple times now.
我正在运行 Mavericks OS X 10.9,所以我不知道这是否是问题所在。我已经尽我所能,但似乎没有任何效果。我已经多次卸载并安装 postgres 和 pg gem。
This is my database.yml file
这是我的 database.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
回答by Chris Slade
It could be as simple as a stale PID file. It could be failing silently because your computer didn't complete the shutdown process completely which means postgresdidn't delete the PID(process id) file.
它可以像陈旧的PID 文件一样简单。它可能会静默失败,因为您的计算机没有完全完成关机过程,这意味着postgres没有删除PID(进程 ID)文件。
The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PIDfile which tells postgresthat another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).
postgres 使用 PID 文件来确保一次只有一个服务器实例正在运行。因此,当它再次启动时,它会失败,因为已经有一个PID文件告诉postgres服务器的另一个实例已启动(即使它没有运行,它也没有关闭并删除 PID) .
- To fix it remove/rename the PID file. Find the postgres data directory. On macOS using homebrew it is in
/usr/local/var/postgres/, or/usr/local/var/log/other systems it might be/usr/var/postgres/. - To make sure this is the problem, look at the log file (
server.log). On the last lines you will see:
- 要修复它,请删除/重命名 PID 文件。找到 postgres 数据目录。在使用自制软件的 macOS 上
/usr/local/var/postgres/,/usr/local/var/log/它在/usr/var/postgres/. - 要确定这是问题所在,请查看日志文件 (
server.log)。在最后几行,您将看到:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
致命:锁定文件“postmaster.pid”已经存在
提示:是否有另一个 postmaster (PID 347) 在数据目录“/usr/local/var/postgres”中运行?
- If so,
rm postmaster.pid Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.
launchctl unload homebrew.mxcl.postgresql.plist launchctl load -w homebrew.mxcl.postgresql.plistOR on newer versions of Brew
brew services restart postgresql
- 如果是这样的话,
rm postmaster.pid 重新启动您的服务器。在使用 launchctl(带有自制软件)的 mac 上,以下命令将重新启动服务器。
launchctl unload homebrew.mxcl.postgresql.plist launchctl load -w homebrew.mxcl.postgresql.plist或在较新版本的Brew 上
brew services restart postgresql
回答by Jai Kumar Rajput
After a lot of searching and analysis, I found a solution if you are using ubuntu just write this command in your terminal and hit enter
经过大量的搜索和分析,我找到了一个解决方案,如果您使用的是 ubuntu,只需在终端中编写此命令并按 Enter
sudo service postgresql restart
This will restart your PostgreSQL, I hope this would be helpful for you.
这将重新启动您的 PostgreSQL,我希望这对您有所帮助。
回答by techdreams
This issue comes when postgres doesn't shut down properly. Here is how I resolved this issue in three simple steps.
当 postgres 未正确关闭时会出现此问题。以下是我如何通过三个简单的步骤解决此问题。
Step 1:Go to your postgres directory
第 1 步:转到您的 postgres 目录
Mac Userswill find this in /usr/local/var/postgres, others might look at /usr/var/postgres/.
Mac 用户会/usr/local/var/postgres在/usr/var/postgres/.
Step 2:Remove .pidfile by running this command.
步骤 2:.pid通过运行此命令删除文件。
rm postmaster.pid
Step 3:Restart your server
第 3 步:重新启动服务器
Mac Users
Mac 用户
brew services restart postgresql
Linux Users
Linux 用户
sudo service postgresql restart
Finally restart your app and you are good to go.
最后重新启动您的应用程序,一切顺利。
回答by Andrea Gherardi
I have managed to solve the problem by following the Chris Slade's answer, but to restart the server, I had to use the following commands:
我已经按照 Chris Slade 的回答设法解决了这个问题,但是要重新启动服务器,我必须使用以下命令:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
that I found here(pjammer's answer down at the bottom)
我在这里找到的(pjammer 的答案在底部)
回答by Mihail Davydenkov
Do you have postgresql installed within your system? If not, then watch Install postgresql. After you successfully integrate postgresqlinto your system you can type something like that in your system terminal:
您的系统中是否安装了 postgresql?如果没有,请观看Install postgresql。成功将postgresql集成到您的系统后,您可以在系统终端中键入类似的内容:
which psql
#=> /usr/bin/psql
After that you need to create a user and database in postgresql like this:
之后,您需要在 postgresql 中创建一个用户和数据库,如下所示:
sudo su - postgres
psql
Then you can see the following within your terminal
然后您可以在终端中看到以下内容
postgres=#
Type there:
在那里输入:
CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;
After you do this, then you need to correct your database.yml. Probably you need something like that:
完成此操作后,您需要更正您的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
Also if you have problems with postgresql it is good idea to check pg_hba.conf
此外,如果您在使用 postgresql 时遇到问题,最好检查pg_hba.conf
回答by K M Rakibul Islam
To fix these type of issues with Postgresand to work with Postgreson Mac OSX, this is probably the BESTand the EASIESTsolution that I have found so far:
要解决这些类型的问题与Postgres的,并与工作的Postgres上Mac OSX,这可能是最好的和最简单的解决方案,我迄今发现:
Just download, install and be happy :)
只需下载,安装并快乐:)
回答by IAmNaN
Uninstall pg:
gem uninstall pgUninstall postgres:
brew uninstall postgresNuke the postgres folder which might be lingering with a bunch of stale stuff it in:
rm -rf /usr/local/var/postgresReboot (maybe unnecessary)
Reinstall pg:
brew install postgresMy comment in Chris Slade's answer starts pg the hard way, now I use brew services which has simplified my life in so many ways:
brew install servicesAnd start pg with it:
brew services start postgresqlReinstall the gem:
gem install pg
卸载 pg:
gem uninstall pg卸载 postgres:
brew uninstall postgres核对 postgres 文件夹,该文件夹中可能存在一堆陈旧的东西:
rm -rf /usr/local/var/postgres重新启动(可能没有必要)
重新安装 pg:
brew install postgres我在 Chris Slade 的回答中的评论以艰难的方式开始,现在我使用 brew 服务,它在很多方面简化了我的生活:
brew install services并用它启动 pg:
brew services start postgresql重新安装宝石:
gem install pg
And bobsyouruncle.
和bobsyouruncle。
回答by Alexis
check the file postgresql.conf(on ubuntuis in /etc/postgresql/X.X/main/postgresql.conf) and look for the line that says:
检查文件postgresql.conf(ubuntu在/etc/postgresql/XX/main/postgresql.conf 中)并查找以下行:
listen_addresses="localhost"
try change it to:
尝试将其更改为:
listen_addresses="*"
it would be accepting every IP's, next check the line that says:
它将接受每个 IP,接下来检查以下行:
port=5432
and check if is the same port of your database.yml, by default on my postgresql-9.2 use 5433instead 5432, don't forget to restartthe postgres server,
并检查是否与您的 database.yml 端口相同,默认情况下在我的 postgresql-9.2 上使用5433而不是5432,不要忘记重新启动postgres 服务器,
Good Luck!
祝你好运!
回答by Neil Atkinson
As described by @Magne, the error PG::ConnectionBad - could not connect to server: Connection refusedcan be presented following a major/minor versionupgrade (e.g. 9.5 -> 9.6or 9 -> 10) of PostgreSQL.
正如@Magne 所描述的,该错误PG::ConnectionBad - could not connect to server: Connection refused可能会在 PostgreSQL的主要/次要版本升级(例如9.5 -> 9.6或9 -> 10)之后出现。
I got this error after having run brew upgrade postgresqlafter the release of PostgreSQL version 9.6. The problem is that major/minor version upgrades require additional steps to migrate old date to the new version.
brew upgrade postgresql在 PostgreSQL 9.6 版发布后运行后出现此错误。问题是主要/次要版本升级需要额外的步骤才能将旧版本迁移到新版本。
How to check if this is your problem
如何检查这是否是您的问题
You can check if this is the problem by checking the the latest brew formula PostgreSQL version installed with homebrew...
您可以通过检查与自制软件一起安装的最新 brew 公式 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
...and then comparing it to the current PG_VERSION
...然后将其与当前的 PG_VERSION 进行比较
$ cat /usr/local/var/postgres/PG_VERSION
9.5
If the PG_VERSION is less than the latest brew formula and the difference is a major/minor version change, then this is probably your problem.
如果 PG_VERSION 小于最新的 brew 公式并且差异是主要/次要版本更改,那么这可能是您的问题。
How to fix (i.e. how to upgrade the data)
如何修复(即如何升级数据)
Instructions below are for an upgrade from 9.5 to 9.6. Change the version numbers as appropriate for your own upgrade
以下说明适用于从 9.5 升级到 9.6。根据您自己的升级情况更改版本号
Step 1.Make sure PostgreSQL is switched off:
步骤 1.确保 PostgreSQL 已关闭:
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql
Step 2.Make a new pristine database:
步骤 2. 创建一个新的原始数据库:
$ initdb /usr/local/var/postgres9.6 -E utf8
Step 3.Check what the old and new binary versions are:
步骤 3.检查旧的和新的二进制版本是什么:
$ ls /usr/local/Cellar/postgresql/
9.5.3 9.5.4 9.6.1
Note that in this example I am upgrading from 9.5.4 binary to 9.6.1 binary
请注意,在此示例中,我将从 9.5.4 二进制升级到 9.6.1 二进制
Step 4.Migrate the current data to the new database using the pg_upgradeutility.
步骤 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
-dflag specifies the current data directory-Dflag specifies the new data directory to be created-bspecifies the old binary-Bspecifies the new binary we're upgrading to
-dflag 指定当前数据目录-Dflag 指定要创建的新数据目录-b指定旧的二进制文件-B指定我们要升级到的新二进制文件
Step 5.Move the old data directory out of the way
步骤 5.将旧数据目录移开
$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
Step 6.Move newly created data directory to where PostgreSQL expects it to be
步骤 6.将新创建的数据目录移动到 PostgreSQL 期望的位置
$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
Step 7.Start PostgreSQL again
步骤 7.再次启动 PostgreSQL
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql
Step 8.If you're using the pg gem for Rails, you should recompile by uninstalling and reinstalling the gem (skip this step if you're not using the pg gem)
步骤 8.如果您使用的是用于 Rails 的 pg gem,您应该通过卸载并重新安装 gem 重新编译(如果您不使用 pg gem,请跳过此步骤)
$ gem uninstall pg
$ gem install pg
Step 9.(optional)After you've reassured yourself that everything is working OK, you can run regain some disk space with the following command:
步骤 9.(可选)在您确信一切正常后,您可以使用以下命令重新获得一些磁盘空间:
brew cleanup postgresql
...and if you're feeling really brave you can delete the old PostgreSQL data directory with the following command
...如果你真的很勇敢,你可以使用以下命令删除旧的 PostgreSQL 数据目录
rm -rf /usr/local/var/postgres9.5/
(This answer is based on an excellent blog post https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/with some additions)
(此答案基于一篇出色的博客文章https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/并添加了一些内容)
回答by Rosana Ruiz
This is what really helped me.
这才是真正帮助我的。
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
Reference: http://alumni.lewagon.org/questions/60

