0

So I have a script that runs like so:

psql -d database_name -f schema.sql 

the schema.sql file contains statements like:

DROP TABLE IF EXISTS networks;
INSERT INTO table(col1, col2. ...) VALUES(val1, val1)

but I have a lot of repeat values in the said script. How do I go about setting and accessing variables?

David Jarrin
  • 1,635
  • 4
  • 19
  • 40
  • How about modifying or creating schema.sql in a shell script? That way, you can create schema.sql with placeholders and a shell script can replace placeholders with values. – zedfoxus Mar 12 '21 at 21:12
  • Something like this? https://stackoverflow.com/questions/36959/how-do-you-use-script-variables-in-psql – richyen Mar 12 '21 at 21:33
  • Does this answer your question? [How do you use script variables in psql?](https://stackoverflow.com/questions/36959/how-do-you-use-script-variables-in-psql) – richyen Mar 12 '21 at 21:33

1 Answers1

0

So things seemed to work for me if I did something like this

DO $$
DECLARE
someVar varchar := 'somevalue...';
anotherVar varchar := 'anothervalue...';
BEGIN
SQL statements including variables
DROP TABLE IF EXISTS networks;
INSERT INTO table(col1, col2. ...) VALUES(somVar, anotherVar)
.
.
.
.
END $$
David Jarrin
  • 1,635
  • 4
  • 19
  • 40