How to execute postgres' sql queries from batch file?

后端 未结 7 1529
小蘑菇
小蘑菇 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:16

    You could pipe it into psql

    (
    echo select * from test;
    ) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% 
    

    When closing parenthesis are part of the SQL query they have to be escaped with three carets.

    ( 
    echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
    ) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
    

提交回复
热议问题