How to import existing *.sql files in PostgreSQL 8.4?

后端 未结 5 1850
悲哀的现实
悲哀的现实 2020-12-04 06:51

I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?

5条回答
  •  一向
    一向 (楼主)
    2020-12-04 07:16

    From the command line:

    psql -f 1.sql
    psql -f 2.sql
    

    From the psql prompt:

    \i 1.sql
    \i 2.sql
    

    Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:

    for f in *.sql ; do psql -f $f ; done
    

    Here's the documentation of the psql application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html

提交回复
热议问题