Importing data from a text-file

You want to import a tab-separated or other formatted text-file.

Solution

Use \copy from psql ref or the COPY command (only as superuser and file-paths need to be relative to the backend) ref.

Discussion

As a general user wanting to import a tab-separated file use something like:

\copy companies FROM /home/richardh/companies.txt
   

The file needs to contain the same columns as the destination table and in the same order. Be careful with non-unix newlines (i.e. a Windows or Mac text-file)

If you have problems, it can be useful to dump existing records and compare this file with the one you are trying to import

\copy companies TO /home/richardh/original-companies.txt
   

If you log in as user postgres, you can use the COPY command which gives you more flexibility, allowing you to define your own delimiters and null-equivalent codes. See the manual for more info.

If importing large numbers of records, drop indexes and restore them afterwards. If importing several tables, you might want to apply foreign-key references at end.

Remember to VACUUM ANALYZE the table afterwards.