【PgSQL管理】命令行下执行SQL

#-c实现命令行下执行SQL

$ psql -h localhost -c "SELECT datname FROM pg_database where datistemplate = false and datname != 'postgres';"
datname
---------
monitor
dw
(2 rows)

#-o输出结果至文件

$ psql -h localhost -c "SELECT datname FROM pg_database where datistemplate = false and datname != 'postgres';" -o db.list
[postgres@t3-jiantou-tkhicomcse-db01-train ~]$ ll
total 5976
-rw-rw-r-- 1 postgres postgres 54 Apr 6 14:21 db.list

#-t, --tuples-only        print rows only

$ psql -h localhost -t -c "SELECT datname FROM pg_database where datistemplate = false and datname != 'postgres';"
monitor
dw

 

posted on 2022-04-06 14:30  HelonTian  阅读(1136)  评论(0编辑  收藏  举报