簡體   English   中英

Postgres 無法連接到服務器

[英]Postgres could not connect to server

在我完成brew updatebrew upgrade之后,我的 postgres 出現了一些問題。 我嘗試卸載 postgres 並重新安裝,但效果不佳。

這是錯誤信息。 (當我嘗試做rake db:migrate時,我也收到了這個錯誤信息)

$ psql
psql: 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"?

我該如何解決?

Mac 版:山獅。

自制軟件版本:0.9.3

postgres 版本:psql (PostgreSQL) 9.2.1

這就是我所做的:

 $ brew uninstall postgresql Uninstalling /usr/local/Cellar/postgresql/9.2.1... $ brew uninstall postgresql Uninstalling /usr/local/Cellar/postgresql/9.1.4... $ psql --version bash: /usr/local/bin/psql: No such file or directory $ brew install postgresql ==> Downloading http://ftp.postgresql.org/pub/source/v9.2.1/postgresql-9.2.1.tar.bz2 Already downloaded: /Library/Caches/Homebrew/postgresql-9.2.1.tar.bz2...... ...... ==> Summary /usr/local/Cellar/postgresql/9.2.1: 2814 files, 38M, built in 2.7 minutes $ initdb /usr/local/var/postgres -E utf8 The files belonging to this database system will be owned by user "laigary". This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8". The default text search configuration will be set to "english". initdb: directory "/usr/local/var/postgres" exists but is not empty If you want to create a new database system, either remove or empty the directory "/usr/local/var/postgres" or run initdb with an argument other than "/usr/local/var/postgres". $ mkdir -p ~/Library/LaunchAgents $ cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/ $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist homebrew.mxcl.postgresql: Already loaded $ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start server starting $ env ARCHFLAGS="-arch x86_64" gem install pg Building native extensions. This could take a while... Successfully installed pg-0.14.1 1 gem installed $ psql --version psql (PostgreSQL) 9.2.1 $ psql psql: 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"?

現在,在我重新安裝自制軟件后,當我使用$ psql時,它沒有顯示任何錯誤消息。

但是我在我的 Rails 應用程序中運行rake db:migrate ,它顯示:

 could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `initialize' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `new' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `connect' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:319:in `checkout_new_connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `block (2 levels) in checkout' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `block in checkout' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in `block in connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:107:in `rescue in create_database' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:51:in `create_database' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (3 levels) in <top (required)>' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `each' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (2 levels) in <top (required)>' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `call' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `each' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `execute' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:144:in `invoke' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `each' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:88:in `top_level' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:66:in `block in run' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling' /usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:63:in `run' /usr/local/bin/rake:32:in `<main>' Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"riy_development", "pool"=>5, "username"=>nil, "password"=>nil}

最后我找到了解決方案。

sudo mkdir /var/pgsql_socket/
sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

這個解決方案有點棘手,但它確實有效。 希望任何人有更好的解決方案

更新

這也適用於我。

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

有類似的問題; pid 文件阻止 postgres 啟動。 要解決這個問題:

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

然后一切都很好。

這有時可能是 postgres 升級的問題。

就我而言,它發生在從 9.3 升級到 9.4 時。

請參閱http://www.postgresql.org/docs/9.4/static/upgrading.html

OS X/自制軟件:

嘗試運行postgres -D /usr/local/var/postgres -- 如果 postgres 無法啟動,它將為您提供更詳細的輸出。

就我而言,運行rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8刪除了我的舊數據庫,然后重新初始化了 postgres db 架構。

感謝https://github.com/Homebrew/homebrew/issues/35240的解決方案。

重新生成我的數據庫(使用rake db:create )后,一切又正常了。

在這里找到了一個對我有用的解決方案:

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

您基本上運行以下命令來手動啟動服務器:

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

如果使用 brew 安裝和卸載 postgres 對您不起作用,請查看您的 postgresql 安裝日志或:

postgres -D /usr/local/var/postgres

如果你看到這種輸出:

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 9.6.1.

然后嘗試以下操作:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

然后啟動服務器:

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

來源

升級數據庫對我有用

brew postgresql-upgrade-database

在 Yosemite 上,如果 pid 文件阻止 Postgres 啟動,並且您有一個launchctl守護進程嘗試(但失敗)加載數據庫守護進程,那么您需要卸載 plist 文件:

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

然后刪除pid文件

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

然后重新加載launchctl守護進程

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

對於閱讀本文並使用 Postgres.app 的任何人,您可能需要在 database.yml 中使用host: localhost http://postgresapp.com/documentation#toc_3

brew services start postgres 

為我工作!

該問題也可以歸因於崩潰的進程,導致 postmaster.pid 文件落后。 我這樣做並工作:

$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
$ brew services start postgresql

檢查套接字文件是否存在。

$ ls -l /tmp/.s.PGSQL.5432
srwxrwxrwx  1 you  wheel  0 Nov 16 09:22 /tmp/.s.PGSQL.5432

如果沒有,請檢查您的 postgresql.conf 是否有 unix_socket_directory 更改。

$ grep unix_socket /usr/local/var/postgres/postgresql.conf
#unix_socket_directory = ''     # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation

問題是因為5432端口上已經有一個正在運行的服務,我們無法通過這個端口建立psql socket連接。

我刪除了套接字文件

rm -rf /tmp/.s.PGSQL.5432/

然后我重新初始化了 postgres 服務

postgres -D /usr/local/var/postgres

這對我有用。

解決這個問題的最愚蠢的方法是做

brew reinstall postgresql

這將使您的用戶權限等保持完整,並且一切都將重置為新的。 一直有效!

這實際上是你應該做的:

你應該看看 /usr/local/var/postgres/postmaster.pid

然后查看文件的第一行 - 這是錯誤的 PID

跑步

ps aux | grep <PID>

例如:

ps aux | grep 12345

然后做

kill <PID>

例如

kill 12345

假設它仍在運行

https://superuser.com/questions/553045/fatal-lock-file-postmaster-pid-already-exists

不要聽已接受的答案,這很糟糕,會損壞您的數據!!!

更改 postresql 或 database.yml 配置設置、更改 $PATH 或創建符號鏈接對我來說都是不必要的。 我需要做的就是gem uninstall pg然后bundle (或gem install pg )。

問題是 pg gem 已在自制 postgres 之前安裝,因此從 MacOS 附帶的 postgres 版本中獲取設置。 重新安裝它(從而重建本機擴展)解決了這個問題。

當我從 9.3.4 升級到 9.5 時,這發生在我身上,因為數據庫不兼容而不升級。

我使用 pg_upgrade 如下:

停止 postgres

$ brew services stop postgresql

升級數據庫:

$ pg_upgrade \
   -d /usr/local/var/postgres \
   -D /usr/local/var/postgres9.5 \
   -b /usr/local/Cellar/postgresql/9.3.4/bin/ \
   -B /usr/local/Cellar/postgresql/9.5.0/bin/ \
   -v

歸檔舊數據庫:

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

重啟 postgres:

 $ brew services start postgresql

更新的寶石(用於導軌 / 活動記錄):

 $ gem uninstall pg
 $ gem uninstall activerecord-postgresql-adapter
 $ bundle install

如果 postgres 是使用自制軟件安裝的,你可以通過運行來解決這個問題:

brew link postgres

這對我有用(作為以前答案的混合):

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

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

資料來源: https : //coderwall.com/p/zf-fww/postgres-on-osx-with-homebrew-not-running-after-osx-crash

Psql 選項

-h 主機名 --host=主機名

:指定運行服務器的機器的主機名。 如果該值以斜杠開頭,則它用作 Unix 域套接字的目錄。

$ grep "port\|unix_socket" /etc/postgresql/9.1/main/postgresql.conf
port = 5433                                         # (change requires restart)
unix_socket_directory = '/var/run/postgresql'       # (change requires resta

$ netstat -nalp | grep postgres
unix  2      [ ACC ]     STREAM     LISTENING     106753   4349/postgres       /tmp/.s.PGSQL.5432
unix  2      [ ACC ]     STREAM     LISTENING     10377 1031/postgres       /var/run/postgresql/.s.PGSQL.5433

使用 -host 選項運行 psql

$ psql -p 5433 -h /var/run/postgresql

無需制作軟鏈接

在 MacOS Sierra 上也遇到了這個問題,當我們如上所述運行 pg_ctl 時,我們遇到了以下錯誤pg_ctl: no database directory specified and environment variable PGDATA unset 所以我們按照這里的步驟解決了我們的問題,即:

mkdir ~/.postgres

initdb ~/.postgres

pg_ctl -D ~/.postgres start

我遇到了同樣的問題,因為我在代碼中使用了錯誤的 Postgres 用戶名。 我登錄到 postgres psql -d postgres並輸入\\du以獲取角色名稱並更正 Postgres 的用戶名。

所以當你們遇到這個問題時,你們需要確保使用正確的 Postgres 用戶名、密碼、主機名和數據庫......

希望這會幫助任何人

我在psql (PostgreSQL) 9.6.11遇到了同樣的問題。

什么對我有用 -

刪除 postmaster.pid -- rm /usr/local/var/postgresql@9.6/postmaster.pid

重啟 postgres -- brew services restart postgresql@9.6

如果這也不起作用,則運行 -- sudo chmod 700 /usr/local/var/postgresql@9.6

如果您在不退出 psql 的情況下關閉系統,postgres 將不會刪除某些文件。

我在 usr/local/var/postgres 位置沒有找到文件 postmaster.pid

所以我做了以下事情:

brew 服務啟動 postgresql

上面的命令應該讓你啟動 postgres

對於 MacOS BigSur,自制的 postgres:

  • rm /opt/homebrew/var/postgres/postmaster.pid
  • brew services restart postgresql

我在嘗試恢復/刪除/創建數據庫而其他進程正在訪問它們后遇到了這個問題。 MacOSX/Homebrew 修復是:

  1. 關閉所有其他訪問進程rails serverrails consoleguard等...
  2. 使用brew info postgres的命令加載/卸載
  3. 從之前運行恢復/刪除/創建

FWIW 今天發生在我身上,但發生的事情是我當時正在運行 Ubuntu 更新,這可能是更新 Postgres。 更新完成后,我就可以順利連接了。

為了完整起見,我試圖從 Rails 控制台從數據庫中檢索記錄:

development (main):0 > a = MyModel.find 73694
PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

看起來你的 psql 沒有運行。 您應該在連接之前運行它。 您只能使用適用於 Mac OS 的 Postgres.app 來做到這一點。 (下載並安裝這個應用程序http://postgresapp.com )打開應用程序,你已經准備好了一個 PostgreSQL 服務器並等待新的連接。 關閉應用程序,服務器關閉。 你也可以在這里找到這個信息http://www.postgresql.org/download/macosx/ 希望這會幫助你。

這是因為以前的服務器仍在運行,請嘗試關閉所有內容並重新運行您的應用程序。

對於那些使用此命令但不起作用或文件不存在並且正在使用 Ruby on Rails 的人

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

或任何其他命令,只是繼續失敗。

我用 Brew 解決了這個問題。 我不得不用 brew 卸載 2 次,因為在第一次卸載時會保留另一個版本的 postgresql,第二次卸載過程將完成。

使用 Brew 安裝 postgresql

然后刪除、創建和遷移項目的數據庫

(不要忘記啟動postgresql服務器)

¿您最近是否更改了 pg_hba.conf? 如果你只是檢查任何錯字:

"local" 僅用於 Unix 域套接字連接

本地所有所有密碼

IPv4 本地連接:

主機全部 127.0.0.1/32 密碼

IPv6 本地連接:

主機全部 ::1/128 密碼

有時一個簡單的錯誤就會讓我們頭疼。 我希望這會有所幫助,如果我的英語一點都不好,我很抱歉。

當 postgres 服務器未運行時會發生這種情況。 在 MAC 上通過 Homebrew 正確安裝 Postgres 的步驟:

  1. brew install postgres

  2. initdb /Users/<username>/db -E utf8 [這將初始化 postgres 以使用給定目錄作為數據庫目錄。 通常不建議將用戶目錄用於數據庫存儲。 編輯 sudoers 文件以添加 initdb 和類似命令,然后在 /usr/local/var/postgres 上運行 initdb]

  3. pg_ctl -D /Users/<username>/db -l logfile start [在第 2 步成功后,將提示運行第 3 步。此命令手動啟動服務器。]

所以我在rails db:create命令之后偶然發現了這個問題。 在 macOS Catalina 10.15.3 中設置環境。

我檢查的第一件事是讓我來到這里的流程。 在確保所有事情都順利進行並且沒有我可能想到的錯誤之后,我嘗試了這里最流行的解決方案,但似乎沒有一個可行。

到目前為止,我看到的唯一錯誤是

$ psql psql: 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"?

所以我需要一些關於正在發生的事情的更具體的信息。 由於這個原因,我決定查看位於

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

所以打開日志后我看到了這個錯誤

LOG: starting PostgreSQL 12.2 on x86_64-apple-darwin19.3.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit LOG: could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known WARNING: could not create listen socket for "localhost" FATAL: could not create any TCP/IP sockets LOG: database system is shut down

所以這更具有解釋性和具體性。 問題在於 PostgreSQL 無法“看到”並解析 localhost 服務器。

所以接下來我要做的是檢查 /etc/hosts 文件,它的默認內容應該是這樣的:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1          localhost 

在將上述內容與我的內容進行比較后,我發現在我的這一行中有所不同並進行了評論(!)。

#::1             localhost

所以我刪除了保存文件的行前面的# 符號並重新運行

rails db:create 

並且成功啟動了數據庫。

就我而言,問題是由 Mac OS 更新引起的。 升級 PostgreSQL 解決了這個問題。

# upgrade database version solved the trouble
$ brew postgresql-upgrade-database

但是,此錯誤很常見並且有多種可能的原因,您不應該完全依賴我上面的解決方案。

MacOS M1 Monterey 解決方案

rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql

我得到了同樣的錯誤。 事實證明,postgres根本沒有運行(它通常總是在后台運行,但無論出於何種原因,它今天都沒有)。

如果是這種情況,只需在項目目錄的命令行中鍵入postgres

經過大量的來回,它真的歸結為我正在使用的pg gem版本。 在小牛隊, pg版本0.15.1將無法連接到端口5432但版本0.17.1工作正常 - 非常奇怪。

對我來說,這是一個導致問題的apache升級。 我仍然可以在控制台中運行psql或直接從kdevelop調用db。 它還將“host = localhost”添加到連接字符串。

但真正的問題是apache已經改為私有tmp。

解決方案:更新/usr/lib/systemd/system/apache2.service並將PrivateTmp = true更改為PrivateTmp = false。

我正在開發OpenSuse操作系統,但我想在Mac上會發生類似的事情。

我發現在安裝 Postgres 9.6 的最新補丁后,我所有的可執行文件( postgrespsqlpg_dumppg_restore等)都被刪除了,我不得不重新鏈接它們。 幸運的是 Homebrew 可以為你做到這一點:

brew link --force postgresql # (or in my case, `postgresql@9.6`)

嘗試

sudo systemctl start postgresql@12-main

這里我的 postgresql 版本是 12。

我正在我的 MacBook M1 (Air) 上做一個項目並打開了一些選項卡,突然我的 Postgres 服務器停止並開始出現以下錯誤:

psql :無法連接到服務器:連接失敗服務器是否在本地運行並接受 Unix 域套接字“/tmp/.s.PGSQL.5432”上的連接

我嘗試了很多解決方案,例如;

  1. 更新 brew 和 Postgres。
  2. 重新啟動服務器。
  3. 試圖從命令行終止進程。

但遺憾的是,沒有什么對我有用。

最后,我想到了重新啟動我的 mac 。 令我驚訝的是,postgres 服務器開始像以前一樣正常工作。 我認為 Postgres 目錄一定有問題。 但我很高興,它奏效了。

我建議您在重新啟動之前先完成上述選項。

謝謝。

原因

Lion 附帶一個已安裝的 postgres 版本,並默認使用這些二進制文件。 一般來說,您可以通過使用自制 postgres 二進制文件的完整路徑來解決這個問題,但其他程序可能仍然存在問題。

解決方案

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

通過

http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/

安裝后只需兩步即可運行數據庫(在此之前確保您以postgres用戶身份登錄)

安裝目錄/bin/postmaster -D 安裝目錄/pgsql/data

例如:

[postgres@localhost bin]$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data

第 2 步:從安裝路徑運行 psql(檢查您安裝的位置 '#which postgres' 將用於找出安裝位置)

[postgres@localhost bin]$ psql 

我使用了 Bitnami 堆棧並作為非 root 用戶 psql 安裝。 在使用 psql 時,我確實收到了提到的錯誤。

原來,有 2 個版本的psql

  1. 預打包在 linux 發行版中的根級 psql。 這指向 /usr/bin/psql
  2. 由 Bitnami 堆棧安裝的非根級別 psql。 這指向 /bitnami/postgresql/postgresql/bin/psql

您可能想要更改 psql 的別名,使其指向非 root 用戶。

alias psql='/bitnami/postgresql/postgresql/bin/psql'

以上對我做psql -U postgres

data 目錄包含一個舊的 postmaster.pid 文件 / data 目錄包含一個不可讀的 postmaster.pid 文件 PostgreSQL 在 data 目錄中放置了一個名為 postmaster.pid 的文件來存儲 PostgreSQL 服務器進程的進程 id。 如果 PostgreSQL 崩潰,此文件可能包含混淆 PostgreSQL 的舊 pid。 您可以通過刪除 postmaster.pid 文件來解決此問題。 但是,您必須確保 PostgreSQL 確實沒有運行。 打開活動監視器並確保沒有名為“postgres”或“postmaster”的進程。

如果在 PostgreSQL 運行時刪除 postmaster.pid 文件,將會發生不好的事情。

來源: https : //postgresapp.com/documentation/troubleshooting.html

如果您在升級 Postgres 時(或升級后)遇到問題,請按照此處提到的步驟安全地在版本之間傳輸數據,這將解決問題:

使用 Homebrew (macOS) 將 PostgreSQL 9.6.5 升級到 10.0

原因之一是確保磁盤的大小未滿執行命令 postmaster -D 的 postgres 路徑

對我們來說,我們必須在 psql 命令中定義 localhost,如下所示:

psql -h localhost -U postgres

不知道為什么-h不默認為localhost ...

我必須修復目錄的權限:

sudo chmod 0750 /usr/local/var/postgres

對我有用的是我在運行brew services list時有 2 個版本的 PostgreSQL

Name           Status  User           Plist
consul         stopped                
docker-machine stopped                
mysql          stopped                
postgresql     started homebrew.mxcl.postgresql.plist
postgresql@9.6 stopped                
redis          stopped                
runit          stopped                
unbound        stopped                
vault          stopped 

剛剛啟動了其他版本的brew services start postgresql@9.6

就我而言,我正在使用docker-compose運行其他項目,我不得不使用sudo service postgresql stop && docker-compose up -d postgres && docker-compose up rails停止 postgresql

為了在我的新項目中修復這個錯誤,我只需要運行: sudo service postgresql restart然后它就修復了它。

我有 macOS Catalina (10.15.6) 和 PostgreSQL 12。

當我運行brew services start postgresqlbrew說它啟動 PostgreSQL 沒有問題,但是當我得到ps auxw | grep post時它沒有顯示ps auxw | grep post ps auxw | grep post

為了解決這個問題,我從這個鏈接安裝Postgres.app ,然后安裝並按下Init按鈕。

它會找到您的數據庫並將其顯示給您。

有同樣的問題。 升級數據庫成功了! postgresql-upgrade-database

MacOS Big Sur似乎將Application\\ Support文件夾重命名為ApplicationSupport 所以更新的路徑將是:

/Users/<USERNAME>/Library/ApplicationSupport/Postgres/var-10 
(or "/var-<YOUR PG VERSION>")

我只是重新啟動了 postgres:

brew services restart postgresql

無需刪除postmaster.pid

這對我rm /usr/local/var/postgres/postmaster.pidrm /usr/local/var/postgres/postmaster.pid

如果您使用的是M1 mac並且安裝了帶有M1 版本 homebrew 的postgresql ,則此外殼可以工作:

$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql

這對我有用。 我不必刪除任何文件

brew postgresql-upgrade-database

對於 Mac OS Catalina,這有效

$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql

這就是它在我的 MacOS 上為我修復這個錯誤的方式(我曾使用homebrew安裝postgres ):

> cd /opt/homebrew/var
> rm -rf postgres
> initdb ./postgres -E utf8
> pg_ctl -D ./postgres -l logfile start
> rm postgres/postmaster.pid
> brew services restart postgresql
> createdb $(whoami)

對於 macOS Monterey,我使用了brew services restart postgresql

brew install postgresql
createdb mydb
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

brew services restart PostgreSQL
==> Successfully started `postgresql` (label: homebrew.mxcl

對我有用的最快解決方案:

  1. 在為 postgres 配置的端口中查找已在運行的 postgres 進程<\/li><\/ol>
    找到postgres的進程id並殺死它們<\/li><\/ol>
    kill -9 <pid><\/code><\/pre>"

我遇到了同樣的問題,但解決方案不起作用。 我終於通過更新我的 postgres 數據庫來修復它:

brew postgresql-upgrade-database

當我開始課程時,他們提到我需要 set_pg,所以每次創建新終端 window 時都需要這樣做

$set_pg

我通過簡單地重新啟動計算機解決了 ubuntu 中的這個問題。 對於我來說,這個問題的原因是 postgres 沒有優雅地停止(通過sudo kill )。

對於 MacOS Monterey 和 Postgresql@14(路徑不同):
rm /usr/local/var/postgresql@14/postmaster.pid
brew services restart postgresql

對我來說,使用 brew 升級效果很好。

  • 配置: Mac OS - Monterey M1
brew postgresql-upgrade-database

我嘗試了幾乎所有這些答案,這就是讓我開始使用 postgres 的最終方法

  • 跟蹤 postgres 的日志。
    • 該路徑需要根據 postgres 的安裝位置和您的版本進行更新。 我在 m1 Monterey 上使用 postgresql@14 並使用自制軟件安裝它。

    • 我終於找到了我需要使用這篇文章查看的路徑。

    • tail /opt/homebrew/var/log/postgresql@14.log

output 顯示:

2023-02-03 15:33:49.294 CST [82651] FATAL:  could not open directory "pg_notify": No such file or directory
2023-02-03 15:33:49.294 CST [82651] LOG:  database system is shut down
  • go到/目錄下cd opt/homebrew/var/postgresql@14

  • 創建丟失的目錄(也許這對你來說是不同的目錄)

    • mkdir pg_notify
  • 對所有丟失的目錄重復此過程。

    • 我需要為pg_tblspcpg_replslotpg_twophasepg_stat_tmppg_logical/snapshotspg_logical/mappingspg_commit_tspg_snapshotspg_commit_ts但我建議你每次都專門運行 tail 命令以確保你不會丟失不同的目錄和文件我。

最后在創建每個丟失的目錄后重復運行 tail 命令后,我得到了這個 output。

2023-02-03 15:49:18.909 CST [85772] LOG:  redo done at 0/17211D8 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
2023-02-03 15:49:18.914 CST [85771] LOG:  database system is ready to accept connections

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM