title: Postgres: \copy syntax
body: <p>With PostgreSQL 9.5 on CentOS 7, I have created a database named <code>sample</code> along with several tables.  I have <code>.csv</code> data in <code>/home/MyUser/data</code> for each table. 
   For example, there exists <code>TableName.csv</code> for the table <code>"TableName"</code>.</p>
  
  <p>How do I load the csv files into each table?</p>
  
  <hr>
  
  <p>What I've tried doesn't work and I can't figure out what I'm doing wrong.</p>
  
  <p><strong>Load from within the DB</strong></p>
  
  <pre><code>$ psql sample
  
  sample=# COPY "TableName" FROM '/home/MyUser/data/TableName.csv' WITH CSV;
  
  ERROR:  could not open file "/home/MyUser/data/TableName.csv" for reading: Permission denied
  </code></pre>
  
  <p>This implies a file permission problem.  All the files in <code>data/</code> are <code>-rw-r--r--</code> and the directory itself is <code>drwxr-xr-x</code>.  So file permissions shouldn't be the problem (unless I'm missing something).  The internet says that <code>COPY</code> has problems with permissions and to try <code>\copy</code>.  </p>
  
  <p><strong>Load from CLI</strong></p>
  
  <pre><code>$ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH CSV
  
  psql: warning: extra command-line argument "FROM" ignored
  psql: warning: extra command-line argument "/home/MyUser/data/TableName.csv" ignored
  psql: warning: extra command-line argument "WITH" ignored
  psql: warning: extra command-line argument "CSV" ignored
  psql: FATAL:  Peer authentication failed for user "sample"
  </code></pre>
  
  <p>This appears to be a syntax error, but I'm not finding the documentation particularly helpful (<code>man psql</code> then <code>/\copy</code>).  I've also tried the following to the same result.</p>
  
  <pre><code>$ psql \copy sample."TableName" FROM /home/MyUser/data/TableName.csv WITH CSV
  $ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH DELIMITER ','
  </code></pre>
  
  <p>There are several other permutations which yield similar errors.</p>
  
  <p><strong>Web Resources Used</strong></p>
  
  <ul>
  <li><p><a href="https://www.postgresql.org/docs/9.5/static/app-psql.html" rel="noreferrer">https://www.postgresql.org/docs/9.5/static/app-psql.html</a></p></li>
  <li><p><a href="https://www.postgresql.org/docs/9.5/static/sql-copy.html" rel="noreferrer">https://www.postgresql.org/docs/9.5/static/sql-copy.html</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/12856377/the-correct-copy-command-to-load-postgresql-data-from-csv-file-that-has-single-q">The correct COPY command to load postgreSQL data from csv file that has single-quoted data?</a></p></li>
  <li><p><a href="https://soleil4716.wordpress.com/2010/08/19/using-copy-command-in-postgresql/" rel="noreferrer">https://soleil4716.wordpress.com/2010/08/19/using-copy-command-in-postgresql/</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/32188644/can-i-use-copy-command-into-a-function-of-postgresql">Can I use \copy command into a function of postgresql?</a></p></li>
  <li><p><a href="https://wiki.postgresql.org/wiki/COPY" rel="noreferrer">https://wiki.postgresql.org/wiki/COPY</a></p></li>
  </ul>