You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now you are ready to upload your file. Suppose you have a CSV file like this:
53
56
@@ -112,6 +115,41 @@ with open(upload_file, 'rb') as f:
112
115
113
116
A slightly more sophisticated script could read the headers from the CSV and compose the `COPY` command on the fly.
114
117
118
+
### CSV files and column ordering
119
+
120
+
When using the **CSV format, please note that [PostgreSQL ignores the header](https://www.postgresql.org/docs/10/static/sql-copy.html)**.
121
+
122
+
> HEADER
123
+
>
124
+
> Specifies that the file contains a header line with the names of each column in the file. On output, the first line contains the column names from the table, and **on input, the first line is ignored**. This option is allowed only when using CSV format.
125
+
126
+
If the ordering of the columns does not match the table definition, you must specify it as part of the query.
127
+
128
+
For example, if your table is defined as:
129
+
130
+
```sql
131
+
CREATETABLEupload_example (
132
+
the_geom geometry,
133
+
name text,
134
+
age integer
135
+
);
136
+
```
137
+
138
+
but your CSV file has the following structure (note `name` and `age` columns are swapped):
139
+
140
+
```csv
141
+
#the_geom,age,name
142
+
SRID=4326;POINT(-126 54),89,North West
143
+
SRID=4326;POINT(-96 34),99,South East
144
+
SRID=4326;POINT(-6 -25),124,Souther Easter
145
+
```
146
+
147
+
your query has to specify the correct ordering, regardless of the header in the CSV:
148
+
149
+
```sql
150
+
COPY upload_example (the_geom, age, name) FROM stdin WITH (FORMAT csv, HEADER true);
151
+
```
152
+
115
153
### Response Format
116
154
117
155
A successful upload will return with status code 200, and a small JSON with information about the upload.
@@ -166,28 +204,20 @@ The Python to "copy to" is very simple, because the HTTP call is a simple get. T
166
204
167
205
```python
168
206
import requests
169
-
import re
170
207
171
208
api_key = {api_key}
172
209
username = {api_key}
173
-
download_file='upload_example_dl.csv'
210
+
download_filename='download_example.csv'
174
211
q ="COPY upload_example (the_geom, name, age) TO stdout WITH (FORMAT csv, HEADER true)"
175
212
176
-
# request the download, specifying desired file name
0 commit comments