title: How to do a backup from a Postgresql-DB via JDBC?
body: <p>In our application, we've implemented an automatic DB migration triggered from within our code. Now we want to backup the existing DB before doing any migration. </p>
  
  <p>Can anyone explain how to do a full backup of a Postgresql-DB via JDBC from within Java code?</p>
  
  <p><strong>Update: it doesn't work via JDBC.</strong></p>
  
  <p>Here some working code to the response of <a href="https://stackoverflow.com/questions/3810288/how-to-do-a-backup-from-a-postgresql-db-via-jdbc/3810386#3810386">Frank Heikens</a>:</p>
  
  <pre><code>    final List&lt;String&gt; baseCmds = new ArrayList&lt;String&gt;();
      baseCmds.add("/usr/bin/pg_dump");
      baseCmds.add("-h");
      baseCmds.add("hostname");
      baseCmds.add("-p");
      baseCmds.add("5432");
      baseCmds.add("-U");
      baseCmds.add("username");
      baseCmds.add("-b");
      baseCmds.add("-v");
      baseCmds.add("-f");
      baseCmds.add("/path/to/backup.sql");
      baseCmds.add("dbName");
      final ProcessBuilder pb = new ProcessBuilder(baseCmds);
  
      // Set the password
      final Map&lt;String, String&gt; env = pb.environment();
      env.put("PGPASSWORD", "password");
  
      try {
          final Process process = pb.start();
  
          final BufferedReader r = new BufferedReader(
                    new InputStreamReader(process.getErrorStream()));
          String line = r.readLine();
          while (line != null) {
              System.err.println(line);
              line = r.readLine();
          }
          r.close();
  
          final int dcertExitCode = process.waitFor();
  
       } catch (IOException e) {
          e.printStackTrace();
       } catch (InterruptedException ie) {
          ie.printStackTrace();
       }
  </code></pre>