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

£可爱£侵袭症+ 提交于 2020-07-23 07:10:21

问题


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:


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.');
    }
    


来源:https://stackoverflow.com/questions/61942125/backup-restore-postgresql-database-and-setup-localhost-environment-with-larave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!