title: Error while using copy_from in psycopg2 while inserting to a postgresql database
body: <p>Whenever I want to insert data in a pandas dataframe to the into a postgresql database I get this error
  <code>error: extra data after last expected column CONTEXT: COPY recommendations, line 1: &quot;0,4070,&quot;[5963, 8257, 9974, 7546, 11251, 5203, 102888, 8098, 101198, 10950]&quot;&quot;</code></p>
  <p>The dataframe consist of three column, the first and second column are of type integers and the third column is a list of integers.</p>
  <p>I created a table in PostgreSQL using this function below</p>
  <pre><code>def create_table(query: str) -&gt; None:
  &quot;&quot;&quot;
  
  :param query: A string of the query to create table in the database
  :return: None
  &quot;&quot;&quot;
  try:
      logger.info(&quot;Creating the table in the database&quot;)
      conn = psycopg2.connect(host=HOST, dbname=DATABASE_NAME, user=USER, password=PASSWORD, port=PORT)
  
      cur = conn.cursor()
      cur.execute(query)
      conn.commit()
      logger.info(&quot;Successfully created a table in the database using this query {}&quot;.format(query))
      return
  except (Exception, psycopg2.Error) as e:
      logger.error(&quot;An error occurred while creating a table using the query {} with exception {}&quot;.format(query, e))
  finally:
      if conn is not None:
          conn.close()
          logger.info(&quot;Connection closed!&quot;)
  </code></pre>
  <p>The query passed into this function is this:</p>
  <pre><code>create_table_query = '''CREATE TABLE Recommendations
  (id INT NOT NULL,
  applicantId INT NOT NULL,
  recommendation INTEGER[], 
  PRIMARY KEY(id), 
  CONSTRAINT applicantId
  FOREIGN KEY(applicantId)
  REFERENCES public.&quot;Applicant&quot;(id)
  ON DELETE CASCADE
  ON UPDATE CASCADE 
  ); '''
  </code></pre>
  <p>I then use the function below to copy the data frame to the created table in postgres.</p>
  <pre><code>def copy_from_file(df: pd.DataFrame, table: str = &quot;recommendations&quot;) -&gt; None:
      &quot;&quot;&quot;
      Here we are going save the dataframe on disk as
      a csv file, load the csv file
      and use copy_from() to copy it to the table
      &quot;&quot;&quot;
  
      conn = psycopg2.connect(host=HOST, dbname=DATABASE_NAME, user=USER, password=PASSWORD, port=PORT)
      # Save the dataframe to disk
      tmp_df = &quot;./tmp_dataframe.csv&quot;
      df.to_csv(tmp_df, index_label='id', header=False)
      f = open(tmp_df, 'r')
      cursor = conn.cursor()
      try:
          cursor.copy_from(f, table, sep=&quot;,&quot;)
          conn.commit()
      except (Exception, psycopg2.DatabaseError) as error:
          os.remove(tmp_df)
          logger.error(&quot;Error: %s&quot; % error)
          conn.rollback()
          cursor.close()
  
      logger.info(&quot;copy_from_file() done&quot;)
      cursor.close()
      os.remove(tmp_df)
  </code></pre>
  <p>And then I still get this <code>error: extra data after last expected column CONTEXT: COPY recommendations, line 1: &quot;0,4070,&quot;[5963, 8257, 9974, 7546, 11251, 5203, 102888, 8098, 101198, 10950]&quot;&quot;</code> please any recommendations on how to fix this issue? Thanks</p>