37

I have created an index.sql file which contains index creating script for 95 table

for example

DROP INDEX IF EXISTS gtab03_vrctrlid_idx cascade;
CREATE UNIQUE INDEX gtab03_vrctrlid_idx ON gtab03 USING btree (vrctrlid);

I have consolidated all table's index creating script to a file called index.sql I need to run the entire script at a time, is it possible to execute the index.sql file using psql

2
  • 3
    Please use the manual when you need it: postgresql.org/docs/current/static/app-psql.html Commented Jun 2, 2014 at 10:01
  • You might want to consider using a PL/PgSQL function to do this dynamically based on a query against information_schema and a loop over EXECUTE format(...), rather than writing out all the statements manually. Search dba.se for examples. Commented Jun 2, 2014 at 11:03

2 Answers 2

55

Is this what you mean?

\i e:/myFolder/index.sql;
Sign up to request clarification or add additional context in comments.

2 Comments

; should not be included because this is a psql command and not SQL.
That doesn't work for me - parameter \i is not known in psql.
43

Following script worked for me,

psql -U postgres -d mydb -a -f "D:\index.sql" -- Absolute path to .sql file

-U, -a, -f

2 Comments

Obviously...but for the good of the community it is easier if they can run the command as it is provided, without having to go digging into the reference to check what this non-obvious flag does. I see you added it, good on you.
You can also "pipe" it, e.g. psql -U postgres -d mydb < D:\index.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.