【问题标题】:How to insert the Linux command df -ht ext4 output into a database table如何将 Linux 命令 df -ht ext4 输出插入数据库表
【发布时间】:2020-07-08 19:55:30
【问题描述】:

谁能帮我看看如何将 Linux 命令数据插入表中

例子:

postgres@PTFPG01:/root$ df -ht ext4
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        98G   15G   79G  16% /
/dev/sdb1       2.0T  921G 1008G  48% /data

我已经尝试使用插入的管道,但它给了我一个错误:

root@PTFPG01:~# df -ht ext4 | psql -d DBA_Monitoring -p 6432 -U postgres -W Olxoo30 -c " insert into driver_size values()"
psql: warning: extra command-line argument "Olxoo30" ignored
Password:
ERROR:  syntax error at or near ")"
LINE 1:  insert into driver_size values() 

create table driver_size (
Filesystem varchar (50),  
Size varchar (10), 
Used varchar (10),
Avail varchar (10),
Use varchar (10),
Mounted_on  varchar (100)
); 

谁能帮我看看如何将数据插入到表格中?

【问题讨论】:

  • 您真的使用 9.1 吗?那是相当古老的

标签: linux postgresql postgresql-9.1 archlinux


【解决方案1】:

这对你有用吗?

df -ht ext4 | tail -n+2 | sed -E "s/ +/\t/g"  | psql -d DBA_Monitoring -p 6432 -U postgres -W Olxoo30 -c 'copy driver_size from stdin;'

【讨论】:

    【解决方案2】:

    -W 开关不会将您的密码作为参数;它的作用是使程序绕过其他获取密码的机制,提示您从键盘输入密码。您应该安排通过其他机制之一(PGPASSWORD、.pgpass 等)提供密码,而不是绕过它们。

    PostgreSQL 没有支持一个或多个空格作为单个分隔符的 COPY 模式,因此您需要一些其他程序来处理输出。我选择了sed 来做到这一点。如果您的任何字段中包含文字空格,则可能会出现问题。

    由于 COPY 的文本格式不支持 HEADER 选项,我改为使用tail 来删除标题行。将它们放在一起,然后:

    df -ht ext4 | sed 's/\s\s*/\t/g' | tail +2 | psql -c 'copy driver_size from stdin'
    

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多