Backup & Restore PostgreSQL database and setup localhost environment with laravel in windows 7

Viewed 1636

1) open C:\Program Files\PostgreSQL\12\data\pg_hba.conf

change: host all all ::1/128 md5

to

host all all ::1/128 trust

2)open pgAdmin & create a localhost server with username postgres and password will empty

/* For taking a backup or restore a dump of existing database name */

open cmd line and go to C:\Program Files\PostgreSQL\12\bin and press enter

and type below command as required

Take Backup : pg_dump.exe -U postgres -d dbname -f D:\Backup\

 or direct take backup using pgAdmin backup option and store in D:\Backup\<backup-file-name>  

hint: backup file should be tar or dump type

Restore Backup : pg_restore -U postgres -d dbname -1 D:\Backup\

3) In laravel code folder open .env file and add DB_SSLMODE=disable

4) in laravel code folder open config/database.php and for 'pgsql' array

 replace

     'sslmode'=> 'require', 

 to    
    'sslmode' => env('DB_SSLMODE','require'),
1 Answers

How to Backup PostgreSql DB In Laravel

  1. install laravel package using composer.

    composer require spatie/laravel-backup

  2. insert the following line to your backup controller.

    use Spatie\DbDumper\Databases\PostgreSql;

  3. write the following code in your backup controller.

    date_default_timezone_set('EST');
    
    try {
        $this->info('The backup has been started');
        $backup_name = 'backup-' . date('c')  . '.sql';
        $backup_path = 'app/backups/' . $backup_name;
        PostgreSql::create()
            ->setDbName(env('DB_DATABASE'))
            ->setUserName(env('DB_USERNAME'))
            ->setPassword(env('DB_PASSWORD'))
            ->dumpToFile($backup_path);
        $this->info('The backup has been proceed successfully.');
    } catch (ProcessFailedException $exception) {
        logger()->error('Backup exception', compact('exception'));
        $this->error('The backup process has been failed.');
    }
    
Related