How to Connect PostgreSQL Database in Laravel?
Hey Dev,
In this tutorial, I will show you laravel postgresql connection. This tutorial will give you a simple example of how to use postgresql database in laravel. you will learn how to connect postgresql database in laravel. I’m going to show you about laravel postgresql database connection.
Laravel typically utilizes MySQL as its default database connection. However, if you wish to employ an PostgreSQL database, you can easily configure Laravel to do so. In the following steps, I'll explain how to establish a connection to an Postgresql database in Laravel. Let's get started.
You can use this example for PostgreSQL database with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install PostgreSQL
In order to establish a connection with an PostgreSQL database using PHP, you'll need to install the PostgreSQL in ubuntu system. You can do this by executing the following command:
sudo apt update
sudo apt install postgresql
sudo -i -u postgres
CREATE DATABASE laravel;
Step 3: Database Configuration
Now, we need to change database connection on .env file. you can change it as like the following .env file code:
.env
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
1. DB_CONNECTION should be set to pgsql to indicate that you're using PostgreSQL.
2. DB_HOST should be set to the PostgreSQL server's IP address or hostname (usually '127.0.0.1' for a local database).
3. DB_PORT is the port PostgreSQL listens on (5432 is the default).
4. DB_DATABASE should be set to the name of the PostgreSQL database you want to use.
5. DB_USERNAME and DB_PASSWORD should be set to your PostgreSQL username and password.
Now, we are ready to run migration command:
php artisan migrate
Step 4: Create Route
Furthermore, open routes/web.php file and update code on it.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
User::updateOrCreate([
'email' => 'hardik@gmail.com'
],[
'name' => 'Hardik Savani',
'email' => 'hardik@gmail.com',
'password' => bcrypt('123456')
]);
$users = User::get();
dd($users->toArray());
});
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/
Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Encrypt Decrypt Database Fields in Laravel?
- How to Add Longblob Data Type using Laravel Migration?
- Upload Files to MySQL Database in Laravel Tutorial
- How to Get Random Records from Database in Laravel?
- Laravel 10 Import Large CSV File into Database Example
- How to Get Database Name in Laravel?
- How to Store Array in Database Laravel?
- Laravel Store JSON Format Data in Database Example
- Laravel Connect Remote Database using SSH Tunnel Example
- How to Get Single Row from Database in Laravel?
- How to Export Mysql Database using Command Line in Ubuntu?
- How to Create Database Seeder in Laravel?
- How to Get Last Record from Database in Laravel?