How to run postgres sql script from another script?

后端 未结 1 1556
滥情空心
滥情空心 2021-02-12 09:59

I have a bunch of SQL scripts that create tables in the database. Each table is located in a separate file so that editing them is much easier.

I wanted to prepare a sin

相关标签:
1条回答
  • 2021-02-12 10:39

    Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \i syntax. I just tested and is working good on PostgreSQL 9.6:

    \i other_script.sql
    SELECT * FROM table_1;
    SELECT * FROM table_2;
    

    By the @wildplasser comment:

    psql -U postgres -h localhost -d postgres < "path to my main sql file"
    

    From psql's perspective the main_sql file is just stdin, and stdin has no "filename". Use -f filename to submit a file with a name:

    psql -U postgres -h localhost -d postgres -f filename.sql
    

    How to run postgres sql script from another script?

    Seems the same question as: How to import external sql scripts from a sql script in PostgreSQL?

    0 讨论(0)
提交回复
热议问题