flyctl integration to simplify both creation and interaction with your Postgres Fly Apps!
Laravel and PostgreSQL Fly App
To run PostgreSQL as a Fly App, follow our guide here. Afterwards you can connect it with your Laravel Fly App through various means:- Connect From a Laravel Fly App by Attachment
- Connect From a Laravel Fly App Manually
- Connect From a Local Environment
Connect From a Laravel Fly App by Attachment
-
You can Attach your Laravel Fly App to your Postgres Fly App with
fly postgres attach:- This will create a database named after
<laravel-app-name>in your Postgres Fly App - This will create an env variable
DATABASE_URLcontaining a connection string in your Laravel fly app
- This will create a database named after
-
Please make sure the
fly.tomlof your Laravel Fly App uses the postgres driver by specifying it as the value ofDB_CONNECTION:Thanks to the attach command, Postgres connection will now be available to your Laravel Fly App using the auto-generatedDATABASE_URLfrom the attachment process. -
If you are using Laravel version 11, it no longer uses the
DATABASE_URLenv variable name, instead it uses DB_URL. So, please set an additional Fly secret,DB_URLwith the value of theDATABASE_URL.
Connect From a Laravel Fly App Manually
If you decide not to use thefly postgres attach command above and would want to connect to your Laravel Fly App in a manual, grueling step-by-step procedure, I hear you! You’ll have to:
- Manually set up a Database in your Postgres Fly App
- Manually connect your Postgres Database with your Laravel Fly App
Manually set up a Database in your Postgres Fly App
Manually connecting with your Postgres Fly App also means manually creating your Postgres Database. You can interact with your Postgres Fly App usingfly postgres connect.
-
Connect with your Postgres Fly App using its App name.
-
Great, you’re connected! Go ahead and complete your set-up with a new database:
-
Now if you check your database list, you should find the recent database created:
flyctl postgres connect command, simply run the command below instead.
Manually connect to Laravel Fly App
Once you have your running and database configured Postgres Fly App, it’s time to connect with your Laravel Fly App:-
Revise your Laravel Fly App’s
fly.tomlfile to connect with theHostname( your Postgres Fly App’s .internal Address ) from the configuration output, and theDB_NAMEconfigured above: -
Set your Laravel database
DB_USERNAMEandDB_PASSWORDwithfly secretsbased on the configuration output received during your Postgres Fly App creation: -
You’ve finally reached the last part. You can now rest easy and deploy:
Connect From a Local Environment
- You can check out various ways to connect with your Postgres Fly App from your local environment, here.
-
Once you’re locally connected to your Postgres Fly App, setup your Laravel .env file to connect with the Postgres database:
Laravel Possible PGSQL Errors
If you get an error similar to,"ERROR: could not find driver pgsql", make sure that your environment is configured properly with a pgsql driver:
-
First up, check your php.ini file and make sure the driver for Postgres is available and uncommented:
- Next, make sure you have the correct php-pgsql installed for your PHP version. Let’s assume you have a PHP 8.1 running locally, then you must also have the compatible php8.1-pgsql in your environment.
"pg_dump: error: server version: 14.x; pg_dump version: 12.x", the pgsql client running in the VM does not match the server version.
- To fix this, update your
Dockerfile. In there, you can install an updated version of the PostgreSQL clients by adding the following commands:
Test Connection
To test whether you are connected to your sparkling new Postgres Fly App, a simplephp artisan migrate should let you know.
Once migration completes, you can check the tables migrated in your Postgres Fly App: