0

I'm doing a laravel project, when migrate with port 5432 it shows me could not connect error so I change to port 54320 in .env file. Everything seems okay till I make some test query and Homestead.test/api/test displays could not connect error again. Switch back to 5432 and everything is okay. I can only migrate or seed with port 54320 and make some api query with 5432. Here is my .env file.

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=54320
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • are you running "php artisan config:cache" after each changes in .env ?? – boolfalse Oct 08 '19 at 17:49
  • yes I have tried that with no result. Every time I use config:cache I have to route:clear, config:clear and cache:clear or I will get protocol error. – BlurryShadow Oct 08 '19 at 18:00

3 Answers3

2

I solved the problem by changing port forwarding from

54320(host) => 5432(guest) to 5432(host) => 5432(guest)

in Oracle VM VirtualBox Manager. Have no idea why default settings not working in the first place, but at least I no longer need to flip port back and forth.

0

Set these 2 environment variables like this:

DB_HOST=localhost
DB_PORT=5432

Run this for refreshing the env config:

php artisan config:cache

And try again. If you'll get an error, then we'll think about that.

boolfalse
  • 1,892
  • 2
  • 13
  • 14
  • 1
    Hey thanks for the answer but now I got 2 errors `Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432` + `Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432`. Sorry I'm new here and have no idea how to format comment. – BlurryShadow Oct 08 '19 at 18:01
  • Ok, then 1. comment/remove the DB_PORT line from .env, 2. In the "config/databases.php" make sure you have this 'port' => env('DB_PORT', '5432'), inside of 'pgsql' array. 3. run "php artisan config:cache" 4. retry again.... I think this will solve the problem – boolfalse Oct 08 '19 at 18:19
  • I'm sorry, tried it and still got the same errors. It's weird that I can still run migration fine with port 54320. – BlurryShadow Oct 08 '19 at 18:28
  • Are you using Docker?? By the way, please check your local IP like this ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' or like this curl -4 icanhazip.com or with ipconfig And paste that IP as your DB_HOST Run "php artisan config:cache" and retry – boolfalse Oct 08 '19 at 18:39
  • No I'm using laravel with homestead on windows 10. I tried all ip addresses found in ipconfig, 1 local and 2 virtualbox, each showing same error checking if that address is accepting connection on port 5432. – BlurryShadow Oct 08 '19 at 18:51
  • Then I think the reason comes from your Postgres. May something wrong with it. So follow to [this answer](https://stackoverflow.com/questions/37307346/is-the-server-running-on-host-localhost-1-and-accepting-tcp-ip-connections#answer-37307420) for similar problem – boolfalse Oct 08 '19 at 18:55
0

Believe it or not, this can also be caused by our dear friend, SELinux.

These steps got it working for me:

  • Enable TCP/IP connections for httpd
    setsebool -P httpd_can_network_connect 1
    setsebool -P httpd_can_network_connect_db 1
    
  • (Optional) Allow httpd access to home directories
    setsebool -P httpd_enable_homedirs 1
    
  • Set SELinux context for files being served
    chcon -R -t httpd_sys_content_t /path/to/web/files
    
  • Give httpd rw access to Laravel's storage directory
    chcon -R -t httpd_sys_rw_content_t /path/to/web/files/storage
    

References

Community
  • 1
  • 1
agentile1990
  • 16
  • 1
  • 3