简体   繁体   English

PG::ConnectionBad - 无法连接到服务器:连接被拒绝

[英]PG::ConnectionBad - could not connect to server: Connection refused

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.我正在运行 Mavericks OS X 10.9,所以我不知道这是否是问题所在。 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.我已经多次卸载并安装 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

It could be as simple as a stale PID file .它可以像陈旧的PID 文件一样简单。 It could be failing silently because your computer didn't complete the shutdown process completely which means postgres didn't delete the PID (process id) file.它可能会静默失败,因为您的计算机没有完全完成关机过程,这意味着postgres没有删除PID (进程 ID)文件。

The PID file is used by postgres to make sure only one instance of the server is running at a time. postgres 使用 PID 文件来确保一次只有一个服务器实例正在运行。 So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).因此,当它再次启动时,它会失败,因为已经有一个PID文件告诉postgres服务器的另一个实例已启动(即使它没有运行,它也没有关闭并删除 PID) .

  1. To fix it remove/rename the PID file.要修复它,请删除/重命名 PID 文件。 Find the postgres data directory.找到 postgres 数据目录。 On macOS using homebrew it is in /usr/local/var/postgres/ , or /usr/local/var/log/ other systems it might be /usr/var/postgres/ .在使用自制软件的 macOS 上,它位于/usr/local/var/postgres//usr/local/var/log/其他系统中,它可能是/usr/var/postgres/
  2. To make sure this is the problem, look at the log file ( server.log ).要确保这是问题所在,请查看日志文件 ( server.log )。 On the last lines you will see:在最后几行,您将看到:

FATAL: lock file "postmaster.pid" already exists致命:锁定文件“postmaster.pid”已经存在
HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?提示:另一个 postmaster (PID 347) 是否在数据目录“/usr/local/var/postgres”中运行?

  1. If so, rm postmaster.pid如果是这样, rm postmaster.pid

  2. Restart your server.重新启动您的服务器。 On a mac using launchctl (with homebrew) the following commands will restart the server.在使用 launchctl(带有自制软件)的 mac 上,以下命令将重新启动服务器。

     brew services restart postgresql

OR on older versions of Brew或在旧版本的Brew 上

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

You would need to restart the Postgresql Server .您需要重新启动 Postgresql 服务器

If you are using ubuntu you can restart Postgresql by following command如果您使用的是 ubuntu,则可以通过以下命令重新启动Postgresql

sudo service postgresql restart

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 的答案在底部)

This issue comes when postgres doesn't shut down properly. 如果postgres无法正常关闭,则会出现此问题。 Here is how I resolved this issue in three simple steps. 这是我通过三个简单步骤解决此问题的方法。

Step 1: Go to your postgres directory 第1步:转到您的postgres目录

Mac Users will 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 .pid file 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. 最后重新启动应用程序,一切顺利。

Do you have postgresql installed within your system?您的系统中是否安装了 postgresql? If not, then watch Install postgresql .如果没有,请观看Install postgresql After you successfully integrate postgresql into your system you can type something like that in your system terminal:成功将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 .执行此操作后,您需要更正database.yml Probably you need something like that:可能你需要这样的东西:

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 checkpg_hba.conf此外,如果您在使用 postgresql 时遇到问题,最好检查pg_hba.conf

  1. Uninstall pg:卸载 pg:

     gem uninstall pg
  2. Uninstall postgres:卸载 postgres:

     brew uninstall postgres
  3. Nuke the postgres folder which might be lingering with a bunch of stale stuff it in:核对 postgres 文件夹,该文件夹中可能存在一堆陈旧的东西:

     rm -rf /usr/local/var/postgres
  4. Reboot (maybe unnecessary)重新启动(可能没有必要)

  5. Reinstall pg:重新安装 pg:

     brew install postgres
  6. My 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:我在 Chris Slade 的回答中的评论以艰难的方式开始,现在我使用 brew 服务,它在很多方面简化了我的生活:

     brew install services
  7. And start pg with it:并用它启动 pg:

     brew services start postgresql
  8. Reinstall the gem:重新安装宝石:

     gem install pg

And bobsyouruncle.和bobsyouruncle。

To fix these type of issues with Postgres and to work with Postgres on Mac OSX , this is probably the BEST and the EASIEST solution that I have found so far:要使用Postgres解决这些类型的问题并在Mac OSX上使用Postgres ,这可能是我迄今为止找到的最好和最简单的解决方案:

http://postgresapp.com/ http://postgresapp.com/

Just download, install and be happy :)只需下载,安装并快乐:)

check the file postgresql.conf (on ubuntu is in /etc/postgresql/XX/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 5433 instead 5432 , don't forget to restart the postgres server,并检查是否与您的 database.yml 端口相同,默认情况下在我的 postgresql-9.2 上使用5433而不是5432 ,不要忘记重新启动postgres 服务器,

Good Luck!祝你好运!

As described by @Magne, the error PG::ConnectionBad - could not connect to server: Connection refused can be presented following a major/minor version upgrade (eg 9.5 -> 9.6 or 9 -> 10 ) of PostgreSQL.正如@Magne 所描述的,错误PG::ConnectionBad - could not connect to server: Connection refused可以在 PostgreSQL 的主要/次要版本升级(例如9.5 -> 9.69 -> 10 )之后出现。

I got this error after having run brew upgrade postgresql after the release of PostgreSQL version 9.6.在 PostgreSQL 9.6 版发布后运行brew upgrade postgresql后出现此错误。 The problem is that major/minor version upgrades require additional steps to migrate old date to the new version.问题是主要/次要版本升级需要额外的步骤才能将旧版本迁移到新版本。

How to check if this is your problem如何检查这是否是您的问题

You can check if this is the problem by checking 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 (ie how to upgrade the data)如何修复(即如何升级数据)

Instructions below are for an upgrade from 9.5 to 9.6.以下说明适用于从 9.5 升级到 9.6。 Change the version numbers as appropriate for your own upgrade根据您自己的升级情况更改版本号

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_upgrade utility.步骤 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 specifies the current data directory -d标志指定当前数据目录
  • -D flag specifies the new data directory to be created -D标志指定要创建的新数据目录
  • -b specifies the old binary -b指定旧的二进制文件
  • -B specifies the new binary we're upgrading to -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/并添加了一些内容)

I got the same problem after updating my mac on Osx Movaje.在 Osx Movaje 上更新我的 mac 后,我遇到了同样的问题。

i found this solution :我找到了这个解决方案:

Try first the bellow command line in your terminal :首先在终端中尝试以下命令行:

brew services restart postgresql

If nothing change :如果没有任何变化:

ps aux | grep postgres

If still nothing change :如果仍然没有任何变化:

ls -ls | grep post

Last command to fix it, removed the postgres lock file by executing from root :修复它的最后一个命令,通过从 root 执行删除 postgres 锁定文件:

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

and then :进而 :

brew services restart postgresql

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

Hope that will help :)希望会有所帮助:)

Regards !!问候 !!

As suggested above, I just opened up the Postgres App on my Mac, clicked Open Psql , closed the psql window, restarted my rails server in my terminal, and it was working again, no more error.如上所述,我刚刚在我的 Mac 上打开Postgres 应用程序,单击 Open Psql ,关闭psql窗口,在我的终端中重新启动我的rails 服务器,它再次工作,没有更多错误。

Trust the elephant: http://postgresapp.com/相信大象: http : //postgresapp.com/

This is what really helped me.这才是真正帮助我的。

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

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

If you hit this problem after doing a brew upgrade which upgraded postgres to a new major version (f.ex 9.3.0 to 9.4.0 or higher), then do this:如果您在执行brew upgrade将 postgres 升级到新的主要版本(f.ex 9.3.09.4.0或更高版本)后遇到此问题,请执行以下操作:

@dmitrygusev's fix from https://github.com/Homebrew/homebrew/issues/35240 @dmitrygusev 的修复来自https://github.com/Homebrew/homebrew/issues/35240

Following official [Postgresql] migration guide helped:以下官方 [Postgresql] 迁移指南有所帮助:

 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

The issue here is that on a major version upgrade of postgres, it's necessary to recreate/migrate your database.这里的问题是在 postgres 的主要版本升级时,有必要重新创建/迁移您的数据库。 And possibly chown directories or manually call initdb .并且可能chown目录或手动调用initdb

See also: How to upgrade PostgreSQL from version 9.6 to version 10.1 without losing data?另请参阅: 如何在不丢失数据的情况下将 PostgreSQL 从 9.6 版升级到 10.1 版?


Some other tips, that might come in handy, in case you're not using Homebrew:如果您不使用 Homebrew,其他一些提示可能会派上用场:

Stop PG server manually:手动停止 PG 服务器:

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

Start PG server manually:手动启动PG服务器:

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

找到您的 postgres 文件,它可能位于/usr/local/var/postgres//usr/var/postgres/ ,然后删除该文件夹中的postmaster.pid文件。

host: localhost放入database.yml文件并运行以下命令:

rake db:create db:migrate  

If removing postmaster.pid does not resolve the issue, this worked for me:如果删除postmaster.pid不能解决问题,这对我有用:

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

Credit to @spirito_libero 's solution on this thread .归功于 @spirito_libero 在此线程上的解决方案。

Mac users with the Postgres app may want to open the application (spotlight search Postgres or find the elephant icon in your menu bar).使用 Postgres 应用程序的 Mac 用户可能想要打开该应用程序(聚焦搜索 Postgres 或在菜单栏中找到大象图标)。 Therein you may see a red X with the message: "Stale postmaster.pid file".您可能会在其中看到带有消息的红色 X:“过时的 postmaster.pid 文件”。 Unfortunately a spotlight search won't show the location of this file.不幸的是,聚光灯搜索不会显示此文件的位置。 Click "Server Settings...", and in the dialog box that opens, click the "Show" button to open the Data Directory.单击“服务器设置...”,在打开的对话框中单击“显示”按钮打开数据目录。 Navigate one folder in (for me it was "var-10"), and delete the postmaster.pid file.导航一个文件夹(对我来说是“var-10”),然后删除postmaster.pid文件。

Go back to the Postgres app and click the Start button.返回 Postgres 应用程序并单击“开始”按钮。 That red X should turn into a green check mark with the message "Running".那个红色的 X 应该变成带有“正在运行”消息的绿色复选标记。 Now you should be able to successfully run Rails commands like rails server in the terminal.现在您应该能够在终端中成功运行 Rails 命令,例如rails server

Postgres 应用服务器设置 - 显示数据目录

I just had this problem tonight, working on a rails application I've been working on for a while.我今晚刚遇到这个问题,正在开发一个我已经工作了一段时间的 Rails 应用程序。 My problem simply came down to the fact that my postgresql server was not running .我的问题只是归结为我的 postgresql 服务器没有运行

I went to the top of the screen (I'm on a Mac) and clicked the little elephant icon and clicked 'Start'.我转到屏幕顶部(我在 Mac 上)并单击小象图标并单击“开始”。

Turns our the server wasn't on.打开我们的服务器没有打开。

Hopefully this provides a simple solution for someone.希望这为某人提供了一个简单的解决方案。

This occurred for me after upgrade Postgres (through Homebrew).这发生在我升级 Postgres 之后(通过 Homebrew)。 I upgraded from version 12 to 13.我从版本 12 升级到 13。

If this is the case for you try running postgres -D /usr/local/var/postgres and check the output.如果是这种情况,您可以尝试运行postgres -D /usr/local/var/postgres并检查输出。 In my case it stated database files are incompatible with server .就我而言,它表示database files are incompatible with server

You might get an error message that postgres.old already exists您可能会收到 postgres.old 已存在的错误消息

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

To fix that, just remove that file要解决这个问题,只需删除该文件

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

Fixing the database was as simple as running:修复数据库就像运行一样简单:

brew postgresql-upgrade-database

I had the same problem, this explanation solved it for me: http://blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/我遇到了同样的问题,这个解释为我解决了: http : //blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/

key step was looking at the tail of my /usr/local/var/postgres/server.log , that let me see what the real problem was, which was that i hadn't fully completed the process of upgrading PostgreSQL关键步骤是查看我的/usr/local/var/postgres/server.log的尾部,这让我看到真正的问题是什么,即我还没有完全完成升级 PostgreSQL 的过程

@Chris Slade's answer helped me. @Chris Slade 的回答对我有帮助。

I wrote a little script to kill those remaining processes if usefull:如果有用,我写了一个小脚本来杀死那些剩余的进程:

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
}

My problem was in my application.yml file.我的问题出在我的application.yml文件中。 My database url on heroku was not using port 5342. Check your heroku config var DATABASE_URL .我在heroku上的数据库url没有使用端口 5342。检查你的heroku配置变量var DATABASE_URL Make sure that matches exactly with was in your application.yml for the applicable database.确保与适用数据库的application.yml中的 was 完全匹配。

我只是运行这个命令sudo service postgresql restart ,一切又sudo service postgresql restart了。

I had the same problem in production (development everything worked), in my case the DB server is not on the same machine as the app, so finally what worked is just to migrate by writing:我在生产中遇到了同样的问题(开发一切正常),在我的情况下,数据库服务器与应用程序不在同一台机器上,所以最后有效的只是通过编写迁移:

bundle exec rake db:migrate RAILS_ENV=production

and then restart the server and everything worked.然后重新启动服务器,一切正常。

I know this is late but might help somebody.我知道这已经晚了,但可能会帮助某人。 I was having the same issue.我遇到了同样的问题。 Turns out that I'd two versions of postgres 9.1 and 9.5.原来我有两个版本的 postgres 9.1 和 9.5。 I uninstalled 9.1 and 9.5 and installed 9.5 again and it worked for me.我卸载了 9.1 和 9.5 并再次安装了 9.5,它对我有用。

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.我正在运行Mavericks OS X 10.9,所以我不知道这是否是问题。 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.我已经多次卸载并安装了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

I had the same problem.我有同样的问题。 I Check the last line of PostgreSQL log files in /var/log/postgresql .我检查/var/log/postgresqlPostgreSQL日志文件的最后一行。 There was an unrecognized configuration parameter in file /etc/postgresql/9.5/main/postgresql.conf .文件/etc/postgresql/9.5/main/postgresql.conf 中有一个无法识别的配置参数。 Commenting the error line in postgresql.conf resolved my problem.注释postgresql.conf的错误行解决了我的问题。

The Homebrew package manager includes launchctl plists to start automatically. Homebrew 包管理器包括 launchctl plists 以自动启动。 For more information run brew info postgres .有关更多信息,请运行brew info postgres

Start manually:手动启动:

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

Stop manually:手动停止:

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

Start automatically:自动启动:

"To have launchd start postgresql now and restart at login:" “现在启动 postgresql 并在登录时重新启动:”

brew services start postgresql

I have tried all of the answers above and it didn't work for me.我已经尝试了上面的所有答案,但对我不起作用。

In my case when I chekced the log on /usr/local/var/log/postgres.log .就我而言,当我检查/usr/local/var/log/postgres.log上的日志时。 It was fine no error.很好,没有错误。 But I could see that it was listening my local IPV6 address which is "::1"但我可以看到它正在侦听我的本地 IPV6 地址,即"::1"

In my database.yml It was like this在我的database.yml是这样的

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

I changed it by我改变了它

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

and then it worked然后它起作用了

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

I ran into this error after following a brew upgrade in which postgresql was updated.在更新了 postgresql 的brew upgrade之后,我遇到了这个错误。 I found exactly how to fix my problem from this great post.我从这篇很棒的帖子中确切地找到了如何解决我的问题。 I was able to get postgres back up and running and even migrated over all my existing databases.我能够让 postgres 备份并运行,甚至迁移到我现有的所有数据库。 https://coderwall.com/p/ti4amw/how-to-launch-postgresql-after-upgrade https://coderwall.com/p/ti4amw/how-to-launch-postgresql-after-upgrade

I just had this problem and none of the suggested solutions worked for me.我刚刚遇到了这个问题,没有一个建议的解决方案对我有用。 After a lot of googling, I did find a solution.经过大量的谷歌搜索,我确实找到了解决方案。 This is what worked for me.这对我有用。

First, I had to run this command to start the server and I am guessing set the location of config file.首先,我必须运行这个命令来启动服务器,我猜测设置了配置文件的位置。

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

Then I ran this command to access postgres然后我运行这个命令来访问 postgres

psql postgres

And at the postgres prompt then I typed "\\du" to list the roles然后在 postgres 提示符下输入“\\du”来列出角色

postgres=# \du

The postgres role was missing so I had to create it with this command缺少 postgres 角色,因此我必须使用此命令创建它

CREATE ROLE POSTGRES WITH SUPERUSER CREATEDB CREATEUSER CREATEROLE REPLICATION BYPASSRLS ;

That solved my problem and I hope this helps someone else.这解决了我的问题,我希望这对其他人有所帮助。

You don't have to delete the postmaster.pid file, as this might be inviting data corruption. 您不必删除postmaster.pid文件,因为这可能会导致数据损坏。

Option? 选项? Simply kill the process (do not use kill -9 , just an ordinary kill will do). 只需kill进程(不要使用kill -9 ,只需执行普通的kill即可)。

Then just restart the postgres server and you're good to go! 然后只需重新启动postgres服务器,您就可以开始了!

Here are the steps to achieve that: 以下是实现该目标的步骤:

  1. Locate and open the postmaster.pid file (mine is on Mac Sierra) 找到并打开postmaster.pid文件(我的设备在Mac Sierra上)

    vi ~/Library/Application\\ Support/Postgres/var-10/postmaster.pid

  2. Copy the PID - it's the number on the first line of the postmaster.pid file 复制PID-它是postmaster.pid文件第一行中的数字

  3. Kill the process with kill PID , eg, if my PID is 381, I will do kill 381 使用kill PID终止进程,例如,如果我的PID为381,我将终止kill 381
  4. Restart Postres - if using brew, do brew services start postgresql . 重新启动Postres-如果使用brew, brew services start postgresql Or if using postgresapp, just simply click the start button 或者,如果使用postgresapp,只需单击“ start按钮

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

Then I had to create postgres user: /usr/local/opt/postgres/bin/createuser -s postgres然后我不得不创建 postgres 用户: /usr/local/opt/postgres/bin/createuser -s postgres

This approach will clobber all of your local data so please back up your data if needed.这种方法会破坏您所有的本地数据,因此请在需要时备份您的数据。

In my case there was permissions issue.就我而言,存在权限问题。

When I see logs I found out the issue, Run当我看到日志时,我发现了问题,运行

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

I found out我发现

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).

I just ran我刚跑

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

It worked.有效。

For MacOS I use this:对于 MacOS,我使用这个:

brew info postgres

And in output in the most end, I see:在最后的输出中,我看到:

...
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

So I just use this command pg_ctl -D /usr/local/var/postgres start and psql begin work.所以我只使用这个命令pg_ctl -D /usr/local/var/postgres startpsql开始工作。

For me, it was really simple: I had a stale postmaster.pid file.对我来说,这真的很简单:我有一个陈旧的postmaster.pid文件。

Simply deleting and restarting postgres fixed it for me:只需删除并重新启动 postgres 即可为我修复它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PG :: ConnectionBad:无法连接到服务器:连接被拒绝 - PG::ConnectionBad: could not connect to server: Connection refused Rails-Postgres-无法连接到服务器:连接被拒绝(PG :: ConnectionBad) - Rails - Postgres - could not connect to server: Connection refused (PG::ConnectionBad) Heroku:PG :: ConnectionBad:无法连接到服务器:连接被拒绝 - Heroku: PG::ConnectionBad: could not connect to server: Connection refused PG::ConnectionBad - 无法连接到服务器:Mac OS X 上的连接被拒绝 - PG::ConnectionBad - could not connect to server: Connection refused on Mac OS X rake 中止:PG::ConnectionBad:无法连接到服务器:连接被拒绝 - rake aborted! PG::ConnectionBad: could not connect to server: Connection refused Rails db:create using PostgreSQL return PG::ConnectionBad: 无法连接到服务器:连接被拒绝 - Rails db:create using PostgreSQL return PG::ConnectionBad: could not connect to server: Connection refused PG :: ConnectionBad:无法连接到服务器:连接被拒绝(Ubuntu 16.04,Rails 5,Capistrano) - PG::ConnectionBad: could not connect to server: Connection refused (Ubuntu 16.04, Rails 5, Capistrano) 部署到heroku时出错:rake资产:预编译PG :: ConnectionBad:无法连接到服务器:连接被拒绝 - Error while deploying to heroku: rake assets:precompile PG::ConnectionBad: could not connect to server: Connection refused PG :: ConnectionBad,无法连接到服务器 - PG::ConnectionBad, could not connect to server PG ConnectionBad 无法连接到服务器 - PG ConnectionBad Could not connect to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM