How To Connect PostgreSQL With Laravel Application

  • 21-05-2022
  • 2777
  • Laravel 9
  • Haresh Chauhan

PostgresSQL is also known as Postgres database, it's a free and open-source relational database management system, It's the more secure and fast database as compared to other databases like MYSQL and other many databases.

PostgresSQL is a powerful database, and it's an open-source object-orient relational database.

While you working with large data, that time Postgres work's more fast and easy load response from server-side. This current version is 14.

This database compiles a large amount of data and easy workload with the database.

This Postgres relational database. Store data point into row with column with different attribute.

Database row stores multiple related rows with different column data attributes. This relational database most common type of database used.

It will also support all that database already in user common database migration like String, Text, LongText, Integer, BigInteger, Time, Date, DateTime, Boolean, etc...

Step: 1 Download Postgres Database.

This is first step move toword connection Postgres database in your laravel application. Please clinic in below link and download Postgres database setup in your system. There will ask to select your operation system and version name. Download PostgreSQL

Once downloaded from the above-given link install it in your system with the default config. They're not and complex process to install, where only ask to enter the database password and confirm the password. Step database password and install Postgres database.

Step: 2 Xampp Extention Enable

After instalation Postgres in your system. You need to change some config in your Xampp file there some default Postgres configuration will have Disabled so we need to enable.

So goto below given path and where you install Xampp in your system. in php.ini file we just need to enable Postgres extension else the database will not work. So Keep In mind.

Enable extention by removing ;

C:\xampp\php

;extension=pdo_pgsql
;extension=pgsql

// To

extension=pdo_pgsql
extension=pgsql

Well!, Now time to restart Xampp If you already started you apache server from Xampp then you just need to restart your apache from Xampp. and start again.

Step: 3 Create Database

Now time to create database in your Postgres database. Press WINDOW key and type pgadmin 4. Select that one and enter. Some processing take simple few seconds.

Once open your Postgres you will see some interface like below exmaple.

image

Goto server and create Database, What name do you want to enter and want a name you give to your database. You can see it in the above image.

Step: 4 .env Config

In my case i have entered my database name laravel, So i have entered my DB_DATABASE=laravel, Ahead My DB_CONNECTION=pgsql because i am using Postgres database that you will see in next step Why i am taking pgsql in DB_CONNECTION. Next my DB_HOST=127.0.0.1 this will alwasy localhost if you are using in your local system else will get from the service provider. DB_POST=5432 default port always 5432 if you entered different while you installed then enter it. DB_USERNAME=postgres this username default for local system else will be provide by when you install at time entered. And last DB_PASSWORD=root this will your password when you entered when you installed Postgres database.

.env

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=postgres
DB_PASSWORD=root

Well!, You successfully connected with PostgreSQL database.

Step: 5 Config.php

Make sure in your config file already have a pgsql config.

config/database.php

'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('DATABASE_URL'),
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'prefix_indexes' => true,
    'schema' => 'public',
    'sslmode' => 'prefer',
],

That will connected by DB_CONNECTION=pgsql above config.

Step: 6 Migrate Database

Migrate your laravel project table with the default user table from your laravel application.

php artisan migrate

It will create table in your database. So let's check where is my table inside database.

Step: 7 Table Path

image

It will create a table in your database. So let's check where is my table inside the database.

You can see your database.

So following above each step and connect PostgreSQL With your laravel application.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :