title: PostgreSQL multiple CSV import and add filename to each column
body: <p>I've got 200k csv files and I need to import them all to a single postgresql table. It's a list of parameters from various devices and each csv's file name contains device's serial number and I need it to be in one of the colums for each row.</p>
  
  <p>So to simplify, I've got few columns of data (no headers), let's say that columns in each csv file are: Date, Variable, Value and file name contains SERIALNUMBER_and_someOtherStuffIDontNeed.csv</p>
  
  <p>I'm trying to use cygwin to write a bash script to iterate over files and do it for me, however for some reason it won't work, showing 'syntax error at or near "as" '</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#!/bin/bash
  FILELIST=/cygdrive/c/devices/files/*
  for INPUT_FILE in $FILELIST
  do
  psql -U postgres -d devices -c "copy devicelist
  (
  Date,
  Variable,
  Value,
  SN as CURRENT_LOAD_SOURCE(),
  )
  from '$INPUT_FILE
  delimiter ',' ;"
  done
  </code></pre>
  
  <p>I'm learning SQL so it might be an obvious mistake, but I can't see it.</p>
  
  <p>Also I know that in that form I will get full file name, not just the serial number bit I want but I can probably handle that somehow later.</p>
  
  <p>Please advise.</p>
  
  <p>Thanks.</p>