I’m a huge fan of Google Cloud Platform because of its intuitive interface and the extremely precise documentation that Google has published regarding its cloud tools. However, there was one error that lost me half an hour of my life when trying to upload a CSV to a PostgreSQL database. Let me walk you through it.
Uploading a CSV to an SQL database in GCP is simple. You create the empty table in your database, you upload the document to a GCP Storage Bucket, and from there you import it into the database table.
First, create your table. I’m importing some data on COVID-19 cases in all US counties. I’m using HeidiSQL to connect to my database, and here’s what my table looks like:
Next, upload your CSV to a bucket. If you haven’t created a bucket yet, here’s how. What’s crucial, is that your CSV does not include headers.
Finally, import your CSV by clicking on Import in the top menu of the dashboard of your Cloud SQL database. This will bring you to the import wizard.
Once you click import, your CSV will be loaded into your database table. Depending on the size of your file, this can take a while.
As I said before, it is crucial that you remove the headers from your CSV file. If all your columns are strings, your headers will be imported as a record. If not all columns are strings (integer in this example), you’ll run into the following error:
ERROR: invalid input syntax for integer: <column name> CONTEXT: COPY import, line 1, column cases: <column name>
Congratulations on importing your CSV into Google Cloud SQL.