Vagrantで立ち上げたPostgreSQLにpsqlで接続できない!?「Psql could not connect to server: No such file or directory, 5432 error」エラーの解決方法

2024-04-02

PostgreSQL Vagrantで発生する「Psql could not connect to server: No such file or directory, 5432 error」エラーの解決方法

Vagrantで立ち上げたPostgreSQLに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"?

原因

このエラーは、以下のいずれかの原因で発生します。

  • PostgreSQLサーバーが起動していない
  • Vagrantの設定が間違っている

解決方法

以下のコマンドを実行して、PostgreSQLサーバーが起動していることを確認します。

sudo service postgresql status

出力例

● postgresql.service - PostgreSQL database server
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-03-08 16:23:13 PST; 11 days ago
       Docs: https://www.postgresql.org/docs/current/
   Main PID: 12345 (postgres)
      Tasks: 1 (limit: 1153)
     CGroup: /system.slice/postgresql.service
             └─12345 /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgresql/14/main

Mar 08 16:23:13 vagrant systemd[1]: Started PostgreSQL database server.

サーバーが起動していない場合

sudo service postgresql start
sudo nano /etc/postgresql/14/main/pg_hba.conf

設定ファイル例

# TYPE  DATABASE    USER        ADDRESS                 METHOD
local   all         postgres                                 peer
host    all         postgres   127.0.0.1/32            md5
host    all         postgres   ::1/128                 md5

確認ポイント

  • local行のMETHODpeerになっていることを確認します。
  • host行のADDRESSが、Vagrantの仮想マシンのIPアドレスになっていることを確認します。

設定に誤りがある場合

sudo service postgresql restart

Vagrantの設定確認

Vagrantfileを確認して、以下の設定が正しく設定されていることを確認します。

config.vm.network "private_network", ip: "192.168.33.10"

Vagrantfileを修正して、Vagrantを再起動します。

vagrant up

その他の解決策

上記の方法で解決できない場合は、以下の解決策を試してみてください。

  • PostgreSQLのログを確認する
  • PostgreSQLを再インストールする

補足

  • 上記の解決方法は、PostgreSQL 14を例に説明しています。他のバージョンの場合は、コマンドや設定ファイルの場所が異なる場合があります。
  • Vagrantの設定は、Vagrantfileによって異なります。



Vagrantfile

# -*- mode: ruby -*-

# Vagrantfile API/syntax version. Don't touch this!
VAGRANTFILE_API_VERSION = "2"

# This is the main entry point for the Vagrantfile. It is called
# when `vagrant up` is run.
Vagrant.configure("2") do |config|

  # The box to use. This can be a box from Vagrant Cloud or
  # a local box.
  config.vm.box = "ubuntu/trusty64"

  # The network to attach the VM to.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Install PostgreSQL
  config.vm.provision "shell", inline: "sudo apt-get update && sudo apt-get install -y postgresql-14"

  # Enable PostgreSQL service to start on boot
  config.vm.provision "shell", inline: "sudo systemctl enable postgresql"

  # Start PostgreSQL service
  config.vm.provision "shell", inline: "sudo service postgresql start"

end

PostgreSQL設定ファイル (/etc/postgresql/14/main/pg_hba.conf)

# TYPE  DATABASE    USER        ADDRESS                 METHOD
local   all         postgres                                 peer
host    all         postgres   127.0.0.1/32            md5
host    all         postgres   ::1/128                 md5

psqlコマンド

psql -h 192.168.33.10 -U postgres -W



PostgreSQL Vagrantで「Psql could not connect to server: No such file or directory, 5432 error」エラーを解決する他の方法

PostgreSQLのデフォルトポート番号は5432ですが、他のポート番号に変更することで、エラーを解決できる場合があります。

手順

  1. PostgreSQLの設定ファイルを開きます。
sudo nano /etc/postgresql/14/main/postgresql.conf

port = 5433
sudo service postgresql restart
  1. psqlコマンドを実行する際に、新しいポート番号を指定します。
psql -h 192.168.33.10 -U postgres -W -p 5433

PostgreSQLには、ローカルソケット接続とTCP/IP接続の2種類の接続方法があります。デフォルトはローカルソケット接続ですが、TCP/IP接続に変更することで、エラーを解決できる場合があります。

sudo nano /etc/postgresql/14/main/pg_hba.conf
  1. host行のMETHODmd5からtrustに変更します。
host    all         postgres   127.0.0.1/32            trust
host    all         postgres   ::1/128                 trust
sudo service postgresql restart
psql -h 127.0.0.1 -U postgres -W
sudo apt-get remove -y postgresql-14
sudo apt-get install -y postgresql-14

Vagrant環境を再構築する

  1. Vagrantを停止します。
vagrant halt
vagrant destroy
vagrant up

専門家に相談する

上記の方法で解決できない場合は、PostgreSQLやVagrantの専門家に相談することを検討してください。


postgresql vagrant


データベースセキュリティ強化!PostgreSQL 読み取り専用ユーザーの活用

PostgreSQL 14以降では、read_onlyという定義済みロールを使用して、簡単に読み取り専用ユーザーを作成できます。この方法では、readonly_userユーザーはSELECTコマンドのみ実行でき、データの変更は一切できません。...


ALTER DATABASEコマンドでPostgreSQLデータベースの所有者を変更する

ALTER DATABASEコマンドを使用するこの方法は、データベース全体、または個々のテーブル、スキーマ、関数の所有者を変更するために使用できます。データベース全体の所有者を変更する例:このコマンドは、my_databaseデータベースの所有者をpostgresユーザーに変更します。...


PostgreSQL で明示的な ID 挿入時に自動インクリメント機能を無効化しない方法:その他の選択肢

PostgreSQLにおいて、テーブルに SERIAL 型の列を定義し、明示的に ID 値を指定してレコードを挿入した場合、自動インクリメント機能が働かず、意図した ID 値が割り当てられない問題が発生することがあります。本記事では、この問題の詳細な原因と解決策について、分かりやすく解説します。...


ウィンドウ関数、サブクエリ、CTE、結合:PostgreSQLで前の非NULL値を取得するベストプラクティス

ウィンドウ関数を使うPostgreSQL 8.4以降では、ウィンドウ関数を使用して前の非NULL値を簡単に取得できます。最も汎用性が高く、柔軟な方法です。このクエリは、your_table テーブルの id 列で順序付けし、各行の value 列の前の非NULL値を previous_value 列として返します。...


SQL SQL SQL SQL Amazon で見る



Mac OS Xで「psql: could not connect to server: No such file or directory (Mac OS X)」エラーを解決する方法

PostgreSQL サーバが起動していない場合は、まずサーバを起動する必要があります。Homebrew でインストールした場合公式パッケージでインストールした場合PostgreSQL サーバの設定ファイル (/etc/postgresql