How to connect supabase with laravel?

Learn how to integrate Supabase with Laravel to leverage the power of an open-source Firebase alternative. This guide provides step-by-step instructions on setting up a secure connection between your Laravel application and Supabase.

Step 1 : Setup Supabase Account

In order to set up a database and storage, you'll require a Supabase account. You can create one by visiting this URL :

Once you've successfully logged in, your next step is to establish a project or database, which should appear somewhat as follows.

Note : Ensure that you securely store your database password in a safe location.

Once the project is created, you'll be redirected to the project dashboard. Here, you need to click on the 'Connect' button, as shown in the image below.

After you click the 'Connect' button, a popup will appear. In the first tab, select 'Connection String', and in the subsequent tab, choose 'URI', as depicted in the image below.

Securely store the copied URI. Then, navigate to the project settings, which can be found in the sidebar. From there, copy the Project URL and the Anonymous Public Key (anon public), and store them in a safe place.

Note : Project URL and the Anonymous Public Key (anon public) will be used for storage, If you are not using supabase storage you can skip it.

We've collected all the necessary information to link our Supabase project with Laravel. Let's proceed with establishing the Laravel connection.

Step 2 : Connect With Laravel

It's presumed that your Laravel project is already up and running. Therefore, please open the .env file. We need to update the database connection details there. Start by deleting the existing database connection and then input the Supabase credentials, Something like this :


If you are going to use Supabase storage, make sure to include the anonymous public key (anon public key) and the project URL as well, Something like this : 


Next, we need to confirm that the psql extension is activated on your server. To do this, open the php.ini file and verify if the following two are enabled.


It's highly likely that this extension is already present in your php.ini file, but it might be commented out with a semicolon (;) at the beginning. Simply remove the semicolon to enable it. If the extension doesn't exist, you can add it after `extension=pdo_mysql`.

Now lets verify if pgsql extension is activated, To do that you can create a basic route function in web.php 

Route::get('phpinfo', function () {
    return phpinfo();

If it's activated, we can then verify if Laravel is successfully connected with Supabase. To do that, let's add a new route function in the web.php.

Route::get('test-connection', function ()
        $pdo = \Illuminate\Support\Facades\DB::connection()->getPDO();
        $dbname = \Illuminate\Support\Facades\DB::connection()->getDatabaseName();
    catch (\Exception $e)

If the database name isn't 'postgres', there may have been an issue. Otherwise, everything is in order and you can migrate your database to Supabase just as you would with MySQL using artisan command, with no differences.


Subscribe Our Newsletters