【问题标题】:PG ConnectionBad Could not connect to serverPG ConnectionBad 无法连接到服务器
【发布时间】:2020-08-14 06:25:28
【问题描述】:

完整的错误是:

PG::ConnectionBad (could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
):


pg (1.2.3) lib/pg.rb:58:in `initialize'
pg (1.2.3) lib/pg.rb:58:in `new'
pg (1.2.3) lib/pg.rb:58:in `connect'
activerecord (6.0.2.2) lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection'
activerecord (6.0.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection'
activerecord (6.0.2.2) lib/active_record/connection_handling.rb:238:in `retrieve_connection'
activerecord (6.0.2.2) lib/active_record/connection_handling.rb:206:in `connection'
activerecord (6.0.2.2) lib/active_record/migration.rb:562:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.2) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.2) lib/rails/engine.rb:526:in `call'
puma (3.12.4) lib/puma/configuration.rb:227:in `call'
puma (3.12.4) lib/puma/server.rb:675:in `handle_request'
puma (3.12.4) lib/puma/server.rb:476:in `process_client'
puma (3.12.4) lib/puma/server.rb:334:in `block in run'
puma (3.12.4) lib/puma/thread_pool.rb:135:in `block in spawn_thread'

我尝试了 rm postmaster.pid 但它没有打印出这样的文件。我也尝试多次重新启动 postgresql,但它一直打印相同的确切错误。 我知道关于这个问题的信息并不多

【问题讨论】:

  • 什么平台?版本?您是否重新启动了整个系统?你能运行psql 并连接吗?请阅读有关如何提问的指南。
  • 我遇到了类似的问题,刚刚发布了我的解决方案here

标签: ruby-on-rails database postgresql


【解决方案1】:

我将向您发送一些帮助我解决此问题的步骤:

  1. 检查 POSTGRES 状态和正在运行的端口(如果在 5432 上):

Ubuntu/Linux 发行版(终端):

$ pg_lsclusters

$ systemctl status postgresql.service

$ service postgresql status

Windows:

Ctrl+R 输入 services.msc

你应该找到 postgres 的状态和它正在运行的端口

  1. 启动 POSTGRES 服务:

Ubuntu/Linux 发行版(终端):

$ systemctl start postgresql.service

$ sudo service postgresql start

Windows

Ctrl + R 键入 services.msc 找到您的 postgres 服务并启动它。

  1. 在 POSTGRES 上检查和创建用户角色

    首先连接数据库:

    $ sudo su postgres

    $ psql

然后检查你的角色是不是用命令创建的:

$ \du

如果尚未创建,您可以使用以下命令创建:

$ CREATE role name_of_your_role_here WITH createdb login password 'your_password_here';

之后你可以再次尝试连接数据库:

别忘了使用

rake db:create
rake db:migrate

并在您的应用程序上配置 database.yml 文件

  1. 在 PG_HBA.CONF 上更改客户端身份验证方法

您可以在此处查看更多详细信息:pg_hba.conf

您可能会使用 md5 密码方法或根本不使用身份验证来进行测试

为此,您可以将本地主机的 METHOD 列从 peer(default) 更改为 md5

您可以在以下位置找到此文件:

LINUX 发行版:

cd /etc/postgresql/你的 postgres 版本在这里/main/pg_hba.conf

WINDOWS:

C:/Porgram Files/PostgresSQL/你的 postgres 版本 她的/data/pg_hba.conf

使用简单用户/密码的选项方法:

TYPE  DATABASE        USER            ADDRESS                 METHOD

IPv4 local connections:

host  all             all             127.0.0.1/32            md5 <= CHANGE HERE

无密码选项方法:

TYPE  DATABASE        USER            ADDRESS                 METHOD

IPv4 local connections:

host  all             all             127.0.0.1/32            trust <= CHANGE HERE

【讨论】:

    【解决方案2】:

    Windows 10

    在我的情况下,PostgreSQL 服务器没有运行,所以我必须按如下方式启动它:

    1. 打开运行应用
    2. 输入services.msc
    3. 找到postgresql-x64-13 - PostgreSQL Server 13(其描述为:提供关系型数据库存储。)
    4. 右键单击,然后选择开始

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 2015-01-31
      • 2016-10-27
      • 2013-11-15
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多