title: Error while fetching data from PostgreSQL COPY from stdin failed
body: <p>I'm using the below Python script to import the CSV file which is placed in the server to a PostgreSQL table.</p>
  
  <p><strong>But i'm getting the below error.</strong></p>
  
  <pre><code>Error while fetching data from PostgreSQL COPY from stdin failed: error in .read() call: UnicodeDecodeError 'utf-8' codec can't decode byte 0xdf in position 1237: invalid continuation byte
  </code></pre>
  
  <p>The CSV file is in "ufl.csv: ISO-8859 text, with very long lines" and my server is in UTF encoding, So can anyone suggest or help me to modify the below script without explicitly converting the CSV file to UTF encoding.Can this be done by code?</p>
  
  <p><strong>The below code is working fine, if i convert the encoding of the CSV file to UTF.</strong></p>
  
  <pre><code>import csv
  import psycopg2
  import time
  import os
  from datetime import datetime
  import shutil
  
  # File path.
  filePath='''/Users/linu/Downloads/ufl.csv'''
  dirName = '/Users/linu/Downloads/ufl_old_files/'
  
  try:
    conn = psycopg2.connect(host="localhost", database="postgres", user="postgres", password="postgres", port="5432")
  
    print('DB connected')
  
  except (Exception, psycopg2.Error) as error:
          # Confirm unsuccessful connection and stop program execution.
          print ("Error while fetching data from PostgreSQL", error)
          print("Database connection unsuccessful.")
          quit()
  
  # Check if the CSV file exists.
  if os.path.isfile(filePath):
   try:
       print('Entered loop')   
       sql = "COPY %s FROM STDIN WITH DELIMITER AS ';'  csv header"
       file = open('/Users/linu/Downloads/ufl.csv', "r")
       table = 'staging.ufl_tracking_details'
  
       with conn.cursor() as cur:
          cur.execute("truncate " + table + ";")
          print('truncated the table')
          cur.copy_expert(sql=sql % table, file=file)
          print('Data loaded')
          conn.commit()
          cur.close()
          conn.close()
  
   except (Exception, psycopg2.Error) as error:
          print ("Error while fetching data from PostgreSQL", error)
          print("Error adding  information.")
          quit()
  
   if not os.path.exists(dirName):
      os.mkdir(dirName)
      print("Directory " , dirName ,  " Created ")
   else:    
      print("Directory " , dirName ,  " already exists")
   tstamp = os.path.getmtime(path)   
   timestamp_name=str(time.time())
   os.rename(filePath,dirName + timestamp_name+'.csv')
  
  else:
      # Message stating CSV file could not be located.
      print("Could not locate the CSV file.")
      quit()
  </code></pre>
  
  <p>Have gone through posts and used "copy_expert" as mentioned in few and tried some other resolutions as well, but none of them worked out . Any hint or suggestions will be of great help.</p>
  
  <p>Note : requirement to port the CSV file and once porting is done move the copied CSV file to a folder and renaming it to name + timestamp.</p>
  
  <p>Thanks in advance</p>