Search code examples
javadatabasepostgresqlpg-dump

Backup of Postgresql database with pg_dump


after a search on the internet with no result, I turn to you! I would like to create a java program, that if you click on a button, it makes a backup of a database in postgresql. I saw that i must use pg_dump but do not understand how to make it work. can someone please help me?

thank you!


Solution

  • if you want to use an OS command inside a Java program, make this (with vivek answer):

    public class Backup{
        public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
            final String cmd = "pg_dump  --format=c --username \"postgres\" db_name > \"D:\\pgBackup\\db_name.backup\"";
    
            java.lang.Runtime rt = java.lang.Runtime.getRuntime();
            java.lang.Process p = rt.exec(cmd);
        }
    }