{"id":3699,"date":"2014-03-29T08:05:23","date_gmt":"2014-03-29T08:05:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/restore-postgresql-database-using-java-collection-of-common-programming-errors\/"},"modified":"2014-03-29T08:05:23","modified_gmt":"2014-03-29T08:05:23","slug":"restore-postgresql-database-using-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/restore-postgresql-database-using-java-collection-of-common-programming-errors\/","title":{"rendered":"Restore PostgreSQL database using java-Collection of common programming errors"},"content":{"rendered":"<p>It&#8217;s surprising that the command you show works at all, since you&#8217;re failing to quote the spaces in the command path. Try:<\/p>\n<pre><code>String[] cmd = {\n    \"D:\\\\Program Files\\\\PostgreSQL\\\\9.1\\\\bin\\\\pg_restore.exe\",\n    \"--host\", \"localhost\",\n    \"--port\", \"5432\",\n    \"--username\", \"postgres\",\n    \"--dbname\", \"mytestqq\",\n    \"--role\", \"postgres\",\n    \"--no-password\",\n    \"--verbose\",\n    \"D:\\\\sathish\\\\rawDatabase.backup\"\n};\np = r.exec(cmd);\n<\/code><\/pre>\n<p>Changes:<\/p>\n<ul>\n<li>Convert the single-string form to the much safer arguments array form of the <code>exec<\/code> call;<\/li>\n<li>Double the backslashes in the <code>rawDatabase<\/code> path since your original command fails to escape backslashes, so <code>\\r<\/code> is a carriage return in the string instead of the <code>\\<\/code> char followed by the <code>r<\/code> char.<\/li>\n<li>Switch to doubled backslashes instead of forward slashes on the program path for consistency. This change probably doesn&#8217;t matter.<\/li>\n<\/ul>\n<p>Also check the return status of the process. You must use <code>Process.waitFor()<\/code> then once it has exited use <code>Process.exitValue()<\/code> to determine the result. You should examine the stderr and stdout captured by the <code>Process<\/code> object for errors and logging information.<\/p>\n<p>The reason your program continues not to work is probably because:<\/p>\n<ul>\n<li>You have old <code>pg_restore<\/code> processes hanging around holding locks; and\/or<\/li>\n<li>You aren&#8217;t consuming stdout and stderr so <code>pg_restore<\/code> runs out of buffered pipe space and blocks writing on the output stream.<\/li>\n<\/ul>\n<p>This will all be much simpler if you use <code>ProcessBuilder<\/code> instead. ProcessBuilder lets you provide file streams to write output to and generally takes care of a lot of this for you. You must still wait for the process to terminate and check its return code though.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s surprising that the command you show works at all, since you&#8217;re failing to quote the spaces in the command path. Try: String[] cmd = { &#8220;D:\\\\Program Files\\\\PostgreSQL\\\\9.1\\\\bin\\\\pg_restore.exe&#8221;, &#8220;&#8211;host&#8221;, &#8220;localhost&#8221;, &#8220;&#8211;port&#8221;, &#8220;5432&#8221;, &#8220;&#8211;username&#8221;, &#8220;postgres&#8221;, &#8220;&#8211;dbname&#8221;, &#8220;mytestqq&#8221;, &#8220;&#8211;role&#8221;, &#8220;postgres&#8221;, &#8220;&#8211;no-password&#8221;, &#8220;&#8211;verbose&#8221;, &#8220;D:\\\\sathish\\\\rawDatabase.backup&#8221; }; p = r.exec(cmd); Changes: Convert the single-string form to the much safer arguments [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3699","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3699"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3699\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}