【问题标题】:How to timestamp data copied into a postgresql database如何为复制到 postgresql 数据库中的数据加上时间戳
【发布时间】:2022-01-03 05:18:08
【问题描述】:

我有一个 shell 脚本将数据从服务器拉到 postgresql 表中。

df -g | awk 'BEGIN{OFS=","}NR>1{$1=$1; print}' > /data/metric.csv


psql -h localhost -d metrics -U postgres -c "copy tablename from STDIN with delimiter as ',';" < /data/metric.csv

显示为:

      filesystem      | gb_blocks |  free  | %used | iused | %iused |   mounted_on
      /dev/hd2             | 16.75     | 12.60  | 25%   | 79098 | 3%     | /usr
      /dev/hd9var          | 8.00      | 6.00   | 25%   | 11965 | 1%     | /var
      /dev/hd3             | 36.75     | 18.83  | 49%   | 5614  | 1%     | /tmp
      /dev/hd1             | 3.25      | 3.11   | 5%    | 674   | 1%     | /home
      /dev/hd11admin       | 0.25      | 0.25   | 1%    | 16    | 1%     | /admin
      /proc                | -         | -      | -     | -     | -      | /proc

我在 Ubuntu 操作系统上使用 Postgresql,并从 AIX 服务器中提取信息。每次将新数据添加到表中时,我想添加一个带有时间戳的列,因为现在它只是混合在一起。我尝试为时间戳添加另一列并为其提供时间戳值,但时间戳不在 csv 文件中,我也不知道如何添加它。我很感激能帮我解决这个问题。

【问题讨论】:

    标签: database postgresql csv


    【解决方案1】:

    创建表并添加带有Default 值的日期列,例如current_Date/now() )

    CREATE TABLE IF NOT EXISTS metrics
    (
        filesystem text ,
        gb_blocks text ,
        free text ,
        per_used text ,
        iused text ,
        per_iused text ,
        mounted_on text ,
        load_dttm timestamp without time zone DEFAULT now()
    );
    

    在加载数据时按以下命令提及columnswith table

    psql -h localhost -d metrics -U postgres -c "copy metrics(filesystem,gb_blocks,free,per_used,iused,per_iused,mounted_on) from STDIN with delimiter as ',';" < /data/metric.csv
    

    【讨论】:

    • @ Rj_N 当我尝试上面的复制命令时,我收到以下错误:错误:“%”处或附近的语法错误 LINE 1: copy smarlowvm(filesystem,gb_blocks,free, %used,使用过,%ius...
    • @SMARZ,在 psql 中尝试复制命令:postgres=# \copy metrics(filesystem,gb_blocks,free,"%used",iused,"%iused",mounted_on) FROM 'metric.csv' DELIMITER ',' CSV
    • 在 psql 中效果很好。我如何修改它以便能够从 psql 外部复制数据?
    • 某些列 %used 没有从 psql 外部获取,您可以考虑重命名没有 % 的列。你可以像 per_used 这样明智地使用应该可以工作。
    • 这行得通。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2013-11-26
    • 2019-09-26
    • 2019-02-11
    • 2012-04-04
    • 2015-04-01
    相关资源
    最近更新 更多