备份与在Windows 7中使用laravel还原PostgreSQL数据库并设置localhost环境
1)打开C:\ Program Files \ PostgreSQL \ 12 \ data \ pg_hba.conf
1) open C:\Program Files\PostgreSQL\12\data\pg_hba.conf
更改:托管所有:: 1:1128 md5
change: host all all ::1/128 md5
到
托管所有:::/128信任
host all all ::1/128 trust
2)打开pgAdmin&用用户名postgres创建一个本地主机服务器,密码将为空
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 */
打开cmd行,然后转到C:\ Program Files \ PostgreSQL \ 12 \ bin,然后按Enter键
open cmd line and go to C:\Program Files\PostgreSQL\12\bin and press enter
并根据需要在命令下方键入
and type below command as required
进行备份:pg_dump.exe -U postgres -d dbname -f D:\ Backup \
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
还原备份:pg_restore -U postgres -d dbname -1 D:\ Backup \
Restore Backup : pg_restore -U postgres -d dbname -1 D:\Backup\
3)在laravel代码文件夹中,打开.env文件并添加DB_SSLMODE = disable
3) In laravel code folder open .env file and add DB_SSLMODE=disable
4)在laravel代码文件夹中打开config/database.php,并打开"pgsql"数组
4) in laravel code folder open config/database.php and for 'pgsql' array
replace
'sslmode'=> 'require',
to
'sslmode' => env('DB_SSLMODE','require'),
如何在Laravel中备份PostgreSql数据库
-
使用composer安装laravel软件包.
install laravel package using composer.
composer需要spatie/laravel-backup
将以下行插入备份控制器.
insert the following line to your backup controller.
使用Spatie \ DbDumper \ Databases \ PostgreSql;
在备份控制器中编写以下代码.
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.');
}