Vagrantで立ち上げたPostgreSQLにpsqlで接続できない!?「Psql could not connect to server: No such file or directory, 5432 error」エラーの解決方法
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
行のMETHOD
がpeer
になっていることを確認します。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ですが、他のポート番号に変更することで、エラーを解決できる場合があります。
手順
- PostgreSQLの設定ファイルを開きます。
sudo nano /etc/postgresql/14/main/postgresql.conf
例
port = 5433
sudo service postgresql restart
- 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
host
行のMETHOD
をmd5
から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環境を再構築する
- Vagrantを停止します。
vagrant halt
vagrant destroy
vagrant up
専門家に相談する
上記の方法で解決できない場合は、PostgreSQLやVagrantの専門家に相談することを検討してください。
postgresql vagrant