How to generate PostgreSQL backup with QProcess, from the Qt app
-
Hi guys, I have the following code. My problem is the following: When I click on my button, I get the message, the backup was generated, but when reviewing the generated file, it is empty. And the other thing is how I pass the server password in the arguments, because I have another postgreSQL instance that has a login password, I would appreciate any suggestions.
void Widget::on_pushButton_6_clicked() { QProcess* process=new QProcess(this); QString prog("C:/Program Files/PostgreSQL/13/bin/pg_dump.exe"); QStringList params; params<<"-U"<<"postgres"<<"-v"<<"-Fc"<<"-f"<<"D:/base.backup"<<"monitoreo_db"; bool res=process->startDetached(prog,params); if(!res){ QMessageBox::critical(this,qApp->applicationName(),"Failed to execute command.\n"); return; } QMessageBox::information(this,qApp->applicationName(),"backup was created successfully.\n"+ QString::number(process->exitStatus())); } -
You are starting the process detached from yours. QProcess::startDetached() does not wait for the process to or finish and it report success if it managed to tell Windows to start the pg_dump process without an immediate error. So, you may be looking too soon for the resulting backup file(s), or it could be failing after launch and your process will never hear about it.
It's also possible that the forward slashes to the target file should be native slashes.
For the password you may be able to use "--no-password" and a .pgpass file; perhaps written to a temporary file by your code and identified to the process in the PGPASSFILE environment variable.
-
You are starting the process detached from yours. QProcess::startDetached() does not wait for the process to or finish and it report success if it managed to tell Windows to start the pg_dump process without an immediate error. So, you may be looking too soon for the resulting backup file(s), or it could be failing after launch and your process will never hear about it.
It's also possible that the forward slashes to the target file should be native slashes.
For the password you may be able to use "--no-password" and a .pgpass file; perhaps written to a temporary file by your code and identified to the process in the PGPASSFILE environment variable.
@ChrisW67
Thanks for your answer, as you said change '/' to '\' and in that case if it worked, I generate the backup correctly, the only thing that left me with doubts was with the password, so what I did was remove the password to the postgres user, and with that fix my problem.
thanks anyway for your reply.