DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Connecting Laravel to PostgreSQL on Ubuntu: A Beginner's Guide

If you're building your first Laravel application and trying to connect it to PostgreSQL, you might run into an error like this:

SQLSTATE[08006] [7] connection to server at "127.0.0.1", port 5432 failed:
FATAL: password authentication failed for user "postgres"
Enter fullscreen mode Exit fullscreen mode

I ran into this while setting up a fresh Laravel application on Linux.

At first, I thought the problem was with Laravel, but the error actually tells us something useful: Laravel can reach PostgreSQL, but PostgreSQL is rejecting the database credentials.

In this article, I'll walk through how to connect Laravel to PostgreSQL on Ubuntu, create a dedicated database user, configure Laravel, and troubleshoot authentication errors.

Prerequisites

Before starting, make sure you have the following installed:

  • PHP
  • Composer
  • Laravel
  • PostgreSQL

You can check your PHP installation with:

php -v
Enter fullscreen mode Exit fullscreen mode

Check Composer:

composer -V
Enter fullscreen mode Exit fullscreen mode

And check PostgreSQL:

psql --version
Enter fullscreen mode Exit fullscreen mode

You can also check whether PostgreSQL is running:

sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

If PostgreSQL isn't running, start it with:

sudo systemctl start postgresql
Enter fullscreen mode Exit fullscreen mode

Create a Laravel Project

If you don't already have a Laravel project, you can create one using Composer:

composer create-project laravel/laravel example-app
Enter fullscreen mode Exit fullscreen mode

Then move into the project:

cd example-app
Enter fullscreen mode Exit fullscreen mode

Laravel stores environment-specific configuration in the .env file.

Configure Laravel for PostgreSQL

Open your .env file:

nano .env
Enter fullscreen mode Exit fullscreen mode

For PostgreSQL, your database configuration should look like this:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
Enter fullscreen mode Exit fullscreen mode

There are a few important things to notice here.

PostgreSQL uses port 5432

PostgreSQL normally runs on:

5432
Enter fullscreen mode Exit fullscreen mode

MySQL commonly uses:

3306
Enter fullscreen mode Exit fullscreen mode

So don't use 3306 if you're connecting Laravel to PostgreSQL.

Laravel uses pgsql

For PostgreSQL, Laravel expects:

DB_CONNECTION=pgsql
Enter fullscreen mode Exit fullscreen mode

Not:

DB_CONNECTION=postgres
Enter fullscreen mode Exit fullscreen mode

What Does the Authentication Error Mean?

When I first tested the connection, I used:

psql -h 127.0.0.1 -U postgres -d postgres -W
Enter fullscreen mode Exit fullscreen mode

PostgreSQL returned:

FATAL: password authentication failed for user "postgres"
Enter fullscreen mode Exit fullscreen mode

This can be confusing when you're new to PostgreSQL.

The important thing is that PostgreSQL is actually responding.

The problem isn't that PostgreSQL can't be found.

Instead, PostgreSQL is saying:

I found the server and the user, but the password provided for that user is incorrect.

So changing the host or port won't fix this particular error.

Create a Dedicated PostgreSQL User

Rather than using the PostgreSQL administrator account for Laravel, I decided to create a separate user specifically for the application.

This is a better approach because your application doesn't need to connect using a PostgreSQL superuser.

First, access PostgreSQL through the system postgres account:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

You should see something similar to:

postgres=#
Enter fullscreen mode Exit fullscreen mode

You're now inside the PostgreSQL command-line interface.

Create the Laravel User

Create a new PostgreSQL user:

CREATE USER laravel_user WITH PASSWORD 'your_secure_password';
Enter fullscreen mode Exit fullscreen mode

For example:

CREATE USER laravel_user WITH PASSWORD 'myStrongPassword123';
Enter fullscreen mode Exit fullscreen mode

For a real application, use a strong password and don't commit it to Git.

Create the Laravel Database

Now create the database and make the new user its owner:

CREATE DATABASE laravel OWNER laravel_user;
Enter fullscreen mode Exit fullscreen mode

You can check your databases with:

\l
Enter fullscreen mode Exit fullscreen mode

You should see the laravel database in the list.

Grant Permissions

Connect to the Laravel database:

\c laravel
Enter fullscreen mode Exit fullscreen mode

Then grant the user access to the public schema:

GRANT ALL ON SCHEMA public TO laravel_user;
Enter fullscreen mode Exit fullscreen mode

You can also grant privileges on existing tables and sequences:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO laravel_user;

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO laravel_user;
Enter fullscreen mode Exit fullscreen mode

For future tables and sequences, you can set default privileges:

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT ALL ON TABLES TO laravel_user;

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT ALL ON SEQUENCES TO laravel_user;
Enter fullscreen mode Exit fullscreen mode

When you're finished, exit PostgreSQL:

\q
Enter fullscreen mode Exit fullscreen mode

Test the PostgreSQL Connection

Before going back to Laravel, it's a good idea to test the database connection directly.

Run:

psql -h 127.0.0.1 -U laravel_user -d laravel -W
Enter fullscreen mode Exit fullscreen mode

Enter the password you created earlier.

If the credentials are correct, you should get:

laravel=>
Enter fullscreen mode Exit fullscreen mode

This is an important troubleshooting step.

If the connection works here, you know PostgreSQL is correctly configured and you can focus on Laravel if another problem occurs.

Update Laravel's .env

Now configure Laravel with the new PostgreSQL user:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=myStrongPassword123
Enter fullscreen mode Exit fullscreen mode

Replace the password with the one you actually created.

Clear Laravel's Configuration Cache

Laravel may still have the old database configuration cached.

Run:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

You should see something similar to:

INFO  Configuration cache cleared successfully.
Enter fullscreen mode Exit fullscreen mode

Run the Migrations

Now run:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

If everything is configured correctly, Laravel should connect to PostgreSQL and create its migration tables.

You should see output similar to:

INFO  Preparing database.

Creating migration table ................................ DONE

INFO  Running migrations.

0001_01_01_000000_create_users_table .................... DONE
0001_01_01_000001_create_cache_table .................... DONE
0001_01_01_000002_create_jobs_table ..................... DONE
Enter fullscreen mode Exit fullscreen mode

At this point, Laravel is successfully connected to PostgreSQL.

Understanding the Setup

The final setup looks like this:

Laravel Application
        |
        | PostgreSQL connection
        ↓
127.0.0.1:5432
        |
        ↓
PostgreSQL
        |
        ↓
laravel_user
        |
        ↓
laravel database
Enter fullscreen mode Exit fullscreen mode

And your .env contains:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
Enter fullscreen mode Exit fullscreen mode

Common Mistakes

1. Using the MySQL port

Don't use:

DB_PORT=3306
Enter fullscreen mode Exit fullscreen mode

for PostgreSQL.

Use:

DB_PORT=5432
Enter fullscreen mode Exit fullscreen mode

2. Using the wrong connection name

Don't use:

DB_CONNECTION=postgres
Enter fullscreen mode Exit fullscreen mode

Use:

DB_CONNECTION=pgsql
Enter fullscreen mode Exit fullscreen mode

3. Using root as the PostgreSQL username

If you're coming from MySQL, you might be used to:

DB_USERNAME=root
Enter fullscreen mode Exit fullscreen mode

PostgreSQL commonly has an administrative user called:

postgres
Enter fullscreen mode Exit fullscreen mode

However, your Laravel application doesn't need to use the administrator account.

A dedicated user such as:

laravel_user
Enter fullscreen mode Exit fullscreen mode

is a better choice.

4. Forgetting to clear Laravel's configuration

If you change your .env file and Laravel continues using the old configuration, run:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Then try:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

5. Not testing PostgreSQL separately

If Laravel gives you a database connection error, test PostgreSQL directly first:

psql -h 127.0.0.1 -U laravel_user -d laravel -W
Enter fullscreen mode Exit fullscreen mode

If this fails, the problem is probably with PostgreSQL, the username, password, database, or permissions.

If it succeeds, then you can focus on Laravel's configuration.

Troubleshooting Checklist

Before running:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

make sure:

  • PostgreSQL is running
  • The laravel database exists
  • laravel_user exists
  • The password is correct
  • The user has access to the database
  • PostgreSQL is running on port 5432
  • DB_CONNECTION=pgsql
  • Laravel's configuration cache has been cleared

Then run:

php artisan config:clear
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Connecting Laravel to PostgreSQL for the first time can be confusing because there are several different pieces involved: Laravel, PHP, PostgreSQL, users, passwords, databases, ports, permissions, and environment variables.

The biggest lesson I learned from this setup is that understanding the error is better than randomly changing configuration until something works.

For example, when you see:

FATAL: password authentication failed for user "postgres"
Enter fullscreen mode Exit fullscreen mode

you already know that PostgreSQL is reachable. The problem is authentication.

From there, you can test PostgreSQL independently, create a dedicated application user, verify the credentials, and then connect Laravel.

Hopefully this guide helps another beginner who is setting up Laravel and PostgreSQL on Ubuntu for the first time.

Top comments (0)