How to execute postgres' sql queries from batch file?

后端 未结 7 1532
小蘑菇
小蘑菇 2021-02-08 14:36

I need to execute SQL from batch file. I am executing following to connect to Postgres and select data from table

C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_         


        
7条回答
  •  深忆病人
    2021-02-08 15:28

    Use the -f parameter to pass the batch file name

    C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% -f 'sql_batch_file.sql'
    

    http://www.postgresql.org/docs/current/static/app-psql.html

    -f filename

    --file=filename

    Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.

    If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).

提交回复
热议问题