Linux - Postgres psql检索不需要的表

时间:2014-04-07 19:50:54

标签: bash postgresql shell scripting

我遇到了以下问题:

我需要一个Postgres数据库,通过Nagios Linux发行版获取数据。 我的目的是将生成的SELECT保存为.txt,使用MUTT通过电子邮件发送给我。

到现在为止,我已经完成了:

#!/bin/sh

psql -d roaming -U thdroaming -o saida.txt << EOF
        \d
        \pset border 2 
        SELECT central, imsi, mapver, camel, nrrg, plmn, inoper, natms, cba, cbaz, stall, ownms, imsi_translation, forbrat FROM  vw_erros_mgisp_totalizador
EOF

我的问题是: .txt“saida.txt”给我带来了关于数据库的信息,如下所示:

                          Lista de relações
 Esquema |               Nome               |   Tipo    |    Dono    
---------+----------------------------------+-----------+------------
 public  | apns                             | tabela    | jmsilva
 public  | config_imsis_centrais            | tabela    | thdroaming
 public  | config_imsis_sgsn                | tabela    | postgres
(3 Registers)

+---------+---------+----------+---------+---------+--------+------------+-------+---------+----------+-------+-------+------------------+-----------+
| central |  imsi   |  mapver  |  camel  |  nrrg   |  plmn  |   inoper   | natms |   cba   |   cbaz   | stall | ownms | imsi_translation |  forbrat  |
+---------+---------+----------+---------+---------+--------+------------+-------+---------+----------+-------+-------+------------------+-----------+
| MCTA02  |   20210 |          |         |         |        | INOPER-127 |       |         |          |       |       |                  |           |
| MCTA02  |   20404 |          |         |         |        | INOPER-127 |       |         |          |       |       |                  |           |
| MCTA02  |   20408 |          |         |         |        | INOPER-127 |       |         |          |       |       |                  |           |
| MCTA02  |   20412 |          |         |         |        | INOPER-127 |       |         |          |       |       |                  |           |

.
.
.

如何才能将第一张表格不导入.txt?

1 个答案:

答案 0 :(得分:1)

删除脚本的'\ d'部分,导致列出您在输出顶部看到的数据库中的表。所以你的脚本将成为:

#!/bin/sh

psql -d roaming -U thdroaming -o saida.txt << EOF
        \pset border 2 
        SELECT central, imsi, mapver, camel, nrrg, plmn, inoper, natms, cba, cbaz, stall, ownms, imsi_translation, forbrat FROM  vw_erros_mgisp_totalizador
EOF

要使输出在名为/tmp/output.csv的文件中显示CSV格式,您可以执行以下操作:

#!/bin/sh

psql -d roaming -U thdroaming -o saida.txt << EOF
        \pset border 2 
        COPY (SELECT central, imsi, mapver, camel, nrrg, plmn, inoper, natms, cba, cbaz, stall, ownms, imsi_translation, forbrat FROM  vw_erros_mgisp_totalizador) TO '/tmp/output.csv' WITH (FORMAT CSV)
EOF