'Bd Postgres connection to Laravel?

I had a problem that I could not solve a few days ago, I try to connect with my Postgres bd which is on a server, I work with laravel but it has highlighted an error when I connect to my Postgres bd, this error appears

PDOException in Connector.php line 55: SQLSTATE[08006] [7] could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

error image:

enter image description here

I would like to know if it has happened the same and how to solve it, Thanks!



Solution 1:[1]

Changing the DB_HOST to postgres, helped me to solve mine.

DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=your-database-name
DB_USERNAME=user-name
DB_PASSWORD=your-password

Solution 2:[2]

Hard to tell without your environment file. This error might occur because you are already connecting to postgres,

To check, make sure this line is in your .env file:

DB_PORT=5432

Or, you can remove the line from the .env and let Laravel choose the default according to the connection:

'port' => env('DB_PORT', '5432'),

Another cause for failed connection due to auth is that the env file is looking for 127.0.0.1. Sometimes changing this to localhost will help.

Solution 3:[3]

Change in your .env setup like this:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1  //If you want to use only in localhost or otherwise change according to host server
DB_PORT=5432
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

After changes of .env edit please enter this command in your terminal for clear cache:php artisan config:cache

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Selam Selam
Solution 2 Watercayman
Solution 3 Udhav Sarvaiya