'Vagrant and Postgres on Mac M1 Monterey - psql: error: connection to server on socket

I am moving from an Intel Mac to one running M1. I am trying to get Vagrant running using Laravel Homestead and Parallels.

When I run

vagrant up

everything runs as expected until it tries to provision PostgreSQL. At that point, I run into the error:

==> machine2018: Running provisioner: Creating Postgres Database: socket_wrench (shell)...

machine2018: Running: script: Creating Postgres Database: socket_wrench

machine2018: createdb: error: could not connect to database template1: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory machine2018: Is the server running locally and accepting connections on that socket?

The SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed. The output for this command should be in the log above. Please read the output to determine what went wrong.

Despite ensuring Postgres is running locally on my Mac AND running out of /var/run/postgresql (instead of the default /tmp), I still get this message. And the same is true when I vagrant ssh into my box.

pg_ctl -D /usr/local/var/postgres restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-03-22 12:27:18.024 EDT [16259] LOG:  starting PostgreSQL 14.2 on aarch64-apple-darwin21.3.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.30), 64-bit
2022-03-22 12:27:18.025 EDT [16259] LOG:  listening on IPv6 address "::1", port 5432
2022-03-22 12:27:18.025 EDT [16259] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-03-22 12:27:18.025 EDT [16259] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-03-22 12:27:18.030 EDT [16260] LOG:  database system was shut down at 2022-03-22 12:27:17 EDT
2022-03-22 12:27:18.037 EDT [16259] LOG:  database system is ready to accept connections

Here is my Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

Vagrant.require_version '>= 2.2.4'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))

homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__))
homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__))
afterScriptPath = "after.sh"
aliasesPath = "aliases"

require File.expand_path(confDir + '/scripts/homestead.rb')

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    if File.exist? aliasesPath then
        config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases"
        config.vm.provision "handle_aliases", type: "shell" do |s|
            s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases && chown vagrant:vagrant /home/vagrant/.bash_aliases"
        end
    end

    if File.exist? homesteadYamlPath then
        settings = YAML::load(File.read(homesteadYamlPath))
    elsif File.exist? homesteadJsonPath then
        settings = JSON::parse(File.read(homesteadJsonPath))
    else
        abort "Homestead settings file not found in #{confDir}"
    end

    Homestead.configure(config, settings)

    if File.exist? afterScriptPath then
        config.vm.provision "Run after.sh", type: "shell", path: afterScriptPath, privileged: false, keep_color: true
    end

#     if File.exist? customizationScriptPath then
#         config.vm.provision "Run customize script", type: "shell", path: customizationScriptPath, privileged: false, keep_color: true
#     end

    if Vagrant.has_plugin?('vagrant-hostsupdater')
        config.hostsupdater.remove_on_suspend = false
        config.hostsupdater.aliases = settpo.ings['sites'].map { |site| site['map'] }
    elsif Vagrant.has_plugin?('vagrant-hostmanager')
        config.hostmanager.enabled = true
        config.hostmanager.manage_host = true
        config.hostmanager.aliases = settings['sites'].map { |site| site['map'] }
    elsif Vagrant.has_plugin?('vagrant-goodhosts')
        config.goodhosts.aliases = settings['sites'].map { |site| site['map'] }
    end

    if Vagrant.has_plugin?('vagrant-notify-forwarder')
        config.notify_forwarder.enable = true
    end
end


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source