Postgres COPY for CSV importing
Working on some integration between Geoserver, Django and NCGMP geodatabases, I often need to use PostgreSQL's COPY command to import data from CSV files into PostgreSQL. Here are some tips:
- You need to have the database table in place first. Django can be useful for that, depending on the situation.
- Create the CSV file with strings in double-quotes. Do not allow any null values, instead use a blank string, i.e. "".
- Do not include a header row with column names. However do make sure that columns are in the same order in the CSV as they are in the database table you're importing to.
- Copy command syntax looks like:
su postgres psql -d {database name} copy {destination table name} from '{absolute path to csv file}' with csv;
Don't forget the semicolon!
Groups:
- Login to post comments