title: PostgreSQL COPY of a CSV file breaking before completion. Works with psql \copy but not COPY
body: <p>I'm currently working with what I think to be a pretty large file that I need to ingest into a postgreSQL database. This is a few example rows of the .csv reads that the COPY command uses to insert into the database. The structure of the rows is that sample_id and otu_id are two foreign keys which refer to primary keys in a sample table and an otu table.</p>
  
  <pre><code>sample_id,otu_id,count
  163,2901,0.0
  164,2901,0.0
  165,2901,0.0
  </code></pre>
  
  <p>Which is ingested into the table with the following code using SQLAlchemy:</p>
  
  <pre><code>self._engine.execute(
                      text('''COPY otu.sample_otu from :csv CSV header''').execution_options(autocommit=True),
                      csv=fname)
  </code></pre>
  
  <p>After copying to the table from the .csv file, I query the database for the samples shown and it gives me a result for the sample_id=163, otu_id=2901 but it doesn't give me the rows after that. If I'm correct, the COPY command stops copying after the first error it encounters so my guess is that there's a problem with the sample of id 164 and otu id of 2901.</p>
  
  <p>I've tried the following:</p>
  
  <ol>
  <li><p>There is a valid entry in the otu table for 2901, likewise for the
  sample table id 164 so I don't think it's a missing key error.</p></li>
  <li><p>I have also searched the file for duplicate foreign key combinations and I can't seem to find any.</p></li>
  <li><p>I've tried to only write every second entry into the .csv file that is copied from incase it was something to do with how large the .csv file was but it ended up giving me the same issue but cutting off at the different point. When only copying entries with even otu_ids, the subsequent table query results for otu_ids > 2890 breaks at sample id 152, otu id 2900.</p></li>
  </ol>
  
  <p>I tried using psql's \copy command to manually copy from the .csv file:</p>
  
  <pre><code>\copy sample_otu FROM 'bpaotu-ijpgihw6' WITH DELIMITER  ',' CSV HEADER;
  </code></pre>
  
  <p>This seems to work perfectly fine. The query shows otu_ids past the otu id 2901.</p>
  
  <p>I'm just very confused as to why it breaks there as the .csv rows before and afterward look identical and there are entries as the corresponding primary key values in the foreign tables which it uses.</p>