Laravel PostgreSQL – Tutorial

Laravel PostgreSQL

In this post we will see how to use Laravel with PostgreSQL, we will use the artisan command line and some instructions with PSQL

Laravel PostgreSQL- Create Project

The first thing we need is to create a project from scratch, let’s enter the following command

composer create-project laravel/laravel laravel9_project

Once the project is created we will give all the permissions

sudo chmod -R 777 laravel9_project

Now we will navigate to the root of the project

cd laravel9_project

It’s time to start the development server with artisan

php artisan serve

PostgreSQL Create User

We will connect through a terminal with PostgreSQL, in this example we place the username followed by the name of the database

psql -U jonathan postgres

We will create a new database

create database laravel9_project;

PostgreSQL List Databases

We will list all the databases created

\l

We will use the new database we have created

\c laravel9_project;

We configure the environment variables in the .env file


DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel9_project
DB_USERNAME=jonathan
DB_PASSWORD=123

Now let’s create a controller in laravel framework

php artisan make:controller TestController

Now let’s create a migration

php artisan make:migration CreateTestTable

Finally we execute all the migrations and we will test the connection to postgresql

php artisan migrate

laravel migraciones

Author