Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adrielzxdf's avatar

Connecting to Postgres Database Outside of SSH Session

I have Homestead set up, and I'm trying to run some Codeception tests outside of the SSH session but I can't figure out how to configure it to do so. The tests are running fine within the SSH, but this is the error I get when running outside of it

[PDOException]
  SQLSTATE[08006] [7] 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" (::1) and accepting
    TCP/IP connections on port 5432?

I understand that it is trying to connect to my machine's DB instead of the VM's DB. How can I configure it to connect to my VM's db instead? Do I configure the .env file?

This was previously discussed but the OP was asking a different question. https://laracasts.com/discuss/channels/servers/cannot-connect-to-default-homestead-pgsql-in-laravel-51

0 likes
1 reply
adrielzxdf's avatar
adrielzxdf
OP
Best Answer
Level 1

It turns out to be a port issue. I'm posting what I did to resolve it here in case anyone encounters the same problem.

database.php

'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            *'port'     => env('DB_PORT'),*
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ],

.env

DB_HOST=localhost
*DB_PORT=54320*
DB_DATABASE=db_name
DB_USERNAME=homestead
DB_PASSWORD=secret

Please or to participate in this conversation.