Hi, thanks for your reply.
The file is 15Mb so wouldn't be good to transfer like that would it ?
Ideally, I'd pass the filename to Postgres and it would run the contents.
Main Topics
Browse All TopicsA third party program generates a .sql script in a file which I would like to run.
In my Java app, I already have a connection in a connection pool to the database, and I'd like to use that to run the .sql script in the file by simply calling a query or function.
The alternative is to run it through the command line, which I can do, but it means passing through the DB username and password which seems less secure than just reusing the same connection. I run on a mixture of Windows and Linux too, so if I can just run a postgres query / function that can run a .sql file (which resides on the same server as postgres itself) then that would seem to me to be the best solution.
All my searching so far has not revealed a solution (although no one has said it's not possible either)
Any ideas appreciated.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
But isn't there a difference in memory usage - I'd have to store all 15Mb in memory and pass that rather than Postgres just streaming straight from the file statement by statment (I assume it doesn't load it all to start with).
I could I guess parse the file myself statement by statement, and run each in turn over JDBC but that seems slow and more work.
PostgreSQL server itself does not load SQL from file.
Executing anything outside java is system-specific and non-portable.
Java's IO is fairly optimized and I doubt you will find fundamental differences in execution time. Use two threads - one that extracts statements into fifo and one that submits statements from fifo to database so that there are no extra delay.
Hi gheist and earthman,
Well I wanted to keep it within Postgres, as then that would be portable (let Postgres deal with the OS differences). Executing from the command line even through Java is probably less portable. And passing the .sql file seems more effort.
I guess the answer is - You cannot execute the contents of a file by running a query/function on Postgres.
Yes the workarounds are shelling out to a command line, or parsing the file, but I knew those, and that was why I asked the question to see if there was an easier way.
I guess I will leave it open for a little bit longer in case anyone else knows that there is a way (or that it is defiitely not possible).
Thanks.
Hi,
Another way would be to just open the file in Java,
put only the string from start to ";" into a variable and send that.
Then put the string from ";"+1 to the next ";" into the variable and
send that. Repeat until you reach the end of the file.
This way you don't load 15MB into java which will quite blow the memory
consumption up.
Depending on how the generated file looks like you might just use
readLine(). The second variant below uses this strategy.
Cheers,
za-k/
Business Accounts
Answer for Membership
by: gheistPosted on 2008-01-16 at 02:56:08ID: 20670634
Load file into variable and pass it as a query to your jdbc connection.