简体   繁体   中英

Import data from Excel to PostgreSQL automatically everyday

I have a single Excel file that is updated every morning with the current inventory of a few items. I would like to know if it's possible to connect that Excel file to PostgreSQL and automate a query that will add new rows on the same table on a daily basis. What I would like to get is something like this:

   -------------------------------     
    col1          col2       col 3
   -------------------------------
    09/10/2019    boxes      5
    09/11/2019    boxes      3
    09/12/2019    boxes      2

You can setup a cronjob to call a simple command to do this:

psql -c "COPY tbname FROM '/tmp/the_file.csv' delimiter ';' csv;"

as described here:

https://www.postgresql.org/docs/current/app-psql.html

And cronjob some like this:

crontab -e then add 6   2   *   *   *   {command as described}

In addition to Dieter's answer. You also need to convert your Excel file to CSV format. You can automate it using xls2csv command-line tool

sudo apt-get update
sudo apt-get install catdoc
xls2csv yourExcelFile.xls > converted.csv

And just add before COPY in crontab:

xls2csv yourExcelFile.xls > converted.csv && psql -c "COPY tbname(col1, col2, col3) FROM 'converted.csv' DELIMITER ';' CSV;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM