Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomating database creation using psql
    primarykey
    data
    text
    <p>I have a file that contains SQL commands for the creation of a database.</p> <p>I also have a set of seperate files, each of which contain SQL commands for creating functions. For example:</p> <ul> <li>funcs_algebra.sql</li> <li>funcs_trigonometry.sql</li> <li>funcs_geometry.sql</li> </ul> <p>Lets say my main SQLscript looks like this:</p> <pre><code>CREATE DATABASE mydb; CREATE TABLE foo(id INT, name VARCHAR(32)); CREATE TABLE foobar(id INT, age REAL); -- Commands below to import the functions in the separate files -- funcs_algebra.sql -- funcs_trigonometry.sql -- funcs_geometry.sql </code></pre> <p>I want to know how to include the files in my 'main SQL' so that I have only one file to pass to psql.</p> <p>The objective is to be able to use psql to create the database (complete with functions) by merely passing the commands in this file to psql.</p> <p>Anyone knows how?</p> <p><strong>[Edit]</strong></p> <p>I probably should have stated what I thought was obvious. I want to keep the function related SQL in SEPARATE files so that I have only one source to modify. It is a way of me partitioning logic and keeping things <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a>.</p> <p>The function definitions are used for creating other template databases - so I want to keep them in separate files, but reference the files from within my SQL script.</p> <p>The only other way I can think of doing this is writing a bash script that makes successive calls to psql - first create the database, and then add the functions - not very elegant, and not very DRY. Is there another (more elegant) way?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload