Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL errors on symfony server launch

I am learning Symfony, I create my project with : symfony new myproject --full directly after this I run symfony serve

in the console I am getting thoses errors :

Tailing Web Server log file (/Users/ben/.symfony/log/cd52af540b09d661e4ffb4f5029da4bbaf3586a9.log)
Tailing PHP-FPM log file (/Users/ben/.symfony/log/cd52af540b09d661e4ffb4f5029da4bbaf3586a9/53fb8ec204547646acb3461995e4da5a54cc7575.log)
                                                                                                                        
 [OK] Web server listening                                                                                              
      The Web server is using PHP FPM 7.3.11                                                                            
      https://127.0.0.1:8000                                                                                            
                                                                                                                        

[Web Server ] Jun  3 23:38:48 |DEBUG  | PHP    Reloading PHP versions 
[Web Server ] Jun  3 23:38:48 |DEBUG  | PHP    Using PHP version 7.3.11 (from default version in $PATH) 
[Application] Jun  3 21:29:59 |CRITICA| REQUES Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused    Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432?" at /Users/ben/Desktop/symfonytuto/demo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php line 96 
[Application] Jun  3 21:29:59 |CRITICA| REQUES Exception thrown when handling an exception (Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused   Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? at /Users/ben/Desktop/symfonytuto/demo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php line 96) 
[Application] Jun  3 21:29:59 |CRITICA| PHP    Uncaught Exception: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused     Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? 
[Application] Jun  3 21:37:59 |ERROR  | REQUES Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET https://127.0.0.1:8000/"" at /Users/ben/Desktop/symfonytuto/demo/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136 
[Application] Jun  3 21:38:00 |ERROR  | REQUES Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET https://127.0.0.1:8000/favicon.ico" (from "https://127.0.0.1:8000/")" at /Users/ben/Desktop/symfonytuto/demo/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136 
[Application] Jun  3 21:38:00 |CRITICA| REQUES Exception thrown when handling an exception (Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused   Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? at /Users/ben/Desktop/symfonytuto/demo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php line 96) 
[Application] Jun  3 21:38:00 |CRITICA| PHP    Uncaught Exception: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused     Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? 
[Application] Jun  3 21:38:00 |CRITICA| REQUES Uncaught PHP Exception Doctrine\DBAL\Exception\ConnectionException: "An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused    Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432?" at /Users/ben/Desktop/symfonytuto/demo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php line 96 
[Application] Jun  3 21:38:00 |CRITICA| REQUES Exception thrown when handling an exception (Doctrine\DBAL\Exception\ConnectionException: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused   Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? at /Users/ben/Desktop/symfonytuto/demo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php line 96) 
[Application] Jun  3 21:38:00 |CRITICA| PHP    Uncaught Exception: An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused     Is the server running on host "127.0.0.1" and accepting     TCP/IP connections on port 5432? 
[Web Server ] Jun  3 23:38:48 |INFO   | PHP    listening path="/usr/sbin/php-fpm" php="7.3.11" port=50170
[PHP-FPM    ] Jun  3 23:38:48 |NOTICE | FPM    fpm is running, pid 1240 
[PHP-FPM    ] Jun  3 23:38:48 |NOTICE | FPM    ready to handle connections 

After this the Symfony's default page is loading correctly but when I add a route to the url for example : https://127.0.0.1:8000/testtt I'm getting this error :

An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

(it should be something like 'this route doesnt exist' no ?)

Please tell me how I can give you more info about my problem !

Thanks a lot !

like image 568
Ben Avatar asked Dec 18 '22 11:12

Ben


1 Answers

This question pops up often enough that I think an answer is appropriate until the issue is resolved.

Assume you just created a new Symfony website-skeleton app, added a new controller and started the development server. You navigate to your new route and exceptions are tossed. Either get a 'driver not found' or a 'could connect to server'. Which of course is a bit unexpected since you have not yet done anything with the database.

The problem is that the Doctrine migrations bundle provides a data collector for Symfony's profiler bar that shows up at the bottom of the browser page when running in development mode. The data collector needs to connect to the database in order to collect migrations data. It does not care about the fact that there is no database. It just tries to connect and ends up tossing an exception.

I opened an issue on this. I am still not entirely sure if this is a feature or bug. Feel free to comment on the issue if you like.

This is a fairly recent problem though I have not tracked exactly when it first popped up. Hence many existing tutorials and step by step guides do not mention it. Consider it to be part of your development experience. Learning how to read exceptions is quite important.

There are several solutions. If you plan on using a database then go ahead and create yourself a database and adjust the DATABASE_URL to point to it. The database can be empty. The migration data collector just needs to be able to connect to it. On my local development machine I actually created a database called db_name just to avoid this error message.

A second solution is to just remove the migrations bundle with

composer remove doctrine/doctrine-migrations-bundle

And the problem goes away. If later on you need migrations then just use composer require to re-install it.

like image 131
Cerad Avatar answered Jan 03 '23 15:01

Cerad