[ruby-on-rails] PG :: ConnectionBad-서버에 연결할 수 없습니다 : 연결이 거부되었습니다

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을 여러 번 제거하고 설치했습니다.

이것은 내 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



답변

오래된 PID 파일 처럼 간단 할 수 있습니다 . 컴퓨터가 종료 프로세스를 완전히 완료하지 않아서 자동으로 실패 할 수 있습니다. 이는 postgresPID (프로세스 ID) 파일을 삭제하지 않았 음 을 의미 합니다.

postgres는 PID 파일을 사용하여 한 번에 하나의 서버 인스턴스 만 실행되도록합니다. 따라서 다시 시작 하면 서버의 다른 인스턴스가 시작되었음을 postgres 에 알려주 는 PID 파일 이 이미 있기 때문에 실패합니다 (실행 중이 아니더라도 PID를 종료하고 삭제하지 못했습니다) .

  1. 이를 해결하려면 PID 파일을 제거 / 이름 바꾸기하십시오. postgres 데이터 디렉토리를 찾으십시오. homebrew를 사용하는 macOS의 /usr/local/var/postgres/경우 또는 /usr/local/var/log/다른 시스템에있을 수 있습니다 /usr/var/postgres/.
  2. 이것이 문제인지 확인하려면 로그 파일 ( server.log)을보십시오. 마지막 줄에는 다음이 표시됩니다.

치명적 : 잠금 파일 “postmaster.pid”가 이미 있습니다.
힌트 : 데이터 디렉토리 “/ usr / local / var / postgres”에서 다른 포스트 마스터 (PID 347)가 실행되고 있습니까?

  1. 그렇다면, rm postmaster.pid
  2. 서버를 다시 시작하십시오. launchctl (homebrew 포함)을 사용하는 Mac에서 다음 명령은 서버를 다시 시작합니다.

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

    또는 최신 버전의 Brew

    brew services restart postgresql

답변

많은 검색 및 분석 후 우분투를 사용하는 경우 터미널 에서이 명령을 작성하고 Enter 키를 누르면 해결책을 찾았습니다.

sudo service postgresql restart

PostgreSQL이 다시 시작됩니다. 도움이 되길 바랍니다.


답변

이 문제는 postgres가 제대로 종료되지 않은 경우에 발생합니다. 다음은 간단한 3 단계로이 문제를 해결 한 방법입니다.

1 단계 : postgres 디렉토리로 이동

Mac 사용자 는이 부분을 찾을 /usr/local/var/postgres수 있습니다 /usr/var/postgres/. 다른 사용자 는이 부분을 볼 수 있습니다 .

2 단계 :.pid 이 명령을 실행하여 파일을 제거하십시오 .

rm postmaster.pid

3 단계 : 서버 다시 시작

맥 사용자

brew services restart postgresql

리눅스 사용자

sudo service postgresql restart

마지막으로 앱을 다시 시작하면 좋습니다.


답변

Chris Slade의 답변에 따라 문제를 해결했지만 서버를 다시 시작하려면 다음 명령을 사용해야했습니다.

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

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

내가 여기 에서 찾은 (바닥에 pjammer의 대답)


답변

시스템 내에 postgresql이 설치되어 있습니까? 그렇지 않은 경우 Install 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 를 확인하는 것이 좋습니다


답변

Postgres에서 이러한 유형의 문제 를 해결하고 Postgres on Mac OSX에서 작업 하려면 지금까지 내가 찾은 BESTEASIEST 솔루션 일 것입니다.

http://postgresapp.com/

그냥 다운로드, 설치 및 행복 🙂


답변

  1. pg 제거 :

    gem uninstall pg
  2. postgres 제거 :

    brew uninstall postgres
  3. 쓸모없는 것들로 남아있을 수있는 postgres 폴더를 Nuke하십시오 :

    rm -rf /usr/local/var/postgres
  4. 재부팅 (불필요 할 수 있음)

  5. pg 재설치 :

    brew install postgres
  6. Chris Slade의 답변에 대한 나의 의견은 어려운 방법으로 시작됩니다. 이제 양조 서비스를 사용하여 인생을 여러 가지 방식으로 단순화했습니다.

    brew install services
  7. 그리고 그것으로 pg를 시작하십시오 :

    brew services start postgresql
  8. 보석을 다시 설치하십시오 :

    gem install pg

그리고 bobsyouruncle.