user10664542
user10664542

Reputation: 1316

Cannot import .csv into Cloud SQL from console

I have tried multiple ways to import a .csv file into Cloud SQL (PostgreSQL), with no luck.

psql -f import.sql does not work. It says that I must be a superuser to import, but that \copy works.

So I tried \copy , and that fails. I get:

postgres=> \copy ppw_raw FROM 'H:\data\data.csv' WITH (FORMAT csv);

server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

So finally I try from https://console.cloud.google.com/path-to-sql-instance

I upload the .csv to a storage bucket, then click all the buttons to import the .csv (in storage bucket), to the DB/Table I want, and the user I want to import as (the user that created the table, that owns the database I am selecting),

and I get a popup message:

Import failed:  Import CSV error: ERROR: relation "<my-table-name>"
does not exist.

but indeed the table exists. The create table command was successful, and when I describe the table, it exists.

I have tried three different ways now to import a .csv, I am behind on a very demanding deadline for a report.

Has anyone anywhere been able to successfully import a .csv file into Google Cloud SQL / PostgreSQL by any means?

Upvotes: 2

Views: 3744

Answers (3)

Tutu Kumari
Tutu Kumari

Reputation: 503

Run the commands :

  1. gcloud init : with the help of this set the project-id
  2. gcloud sql connect your_mysql_instance_name --user=root : enter the mysql
  3. create the database and table schema where you want to import the data
  4. Then using import , import the csv to the created database and schema.

Here is the picture which I have done for first two commands in mysql:

enter image description here

enter image description here

enter image description here

Upvotes: 0

Tutu Kumari
Tutu Kumari

Reputation: 503

Run the commands :

  1. gcloud init : with the help of this set the project-id
  2. gcloud sql connect practice-id-1 --user=root : enter the mysql
  3. create the database and table schema where you want to import the data
  4. Then using import , import the csv to the created database and schema.

Here is the picture which I have done for first two commands in mysql:

enter image description here

enter image description here

enter image description here

Upvotes: 0

Andrei Cusnir
Andrei Cusnir

Reputation: 2805

I tried different methods to import a custom CSV file into the CloudSQL (PostgreSQL) database instance. It looks like you are trying to import data to a table that doesn't exists in your database. I had the same error, when I was trying to import to table that didn't exist.

To import CSV file, make sure that:

  • The CSV file have the same amount of columns as the table in the database.
  • The data in the file are properly structured to represent a CSV file.
  • Before importing a CSV file to CloudSQL, first export couple of lines from the table and open the file to see how the data is represented. Compare the data to the CSV file you want to import and make sure that the data is represented the same way. When exporting form console, choose the bucket folder where the CSV will be exported to, choose the CSV option and in SQL query add the following command SELECT * FROM products LIMIT 5;. It will only export the first 5 records from the database, therefore it won't take much time.

To import the file in the CloudSQL (PostgreSQL) database:

First make sure that the table exists in the database. Connect to your CloudSQl (PostgreSQL) instance. Run the command SELECT * FROM pg_catalog.pg_tables; to list all the tables. If you have found your table in the list, use the exact same name for Table when importing the data to the database.

  1. Upload the CSV file in a bucket folder

  2. Find the CloudSQL (PostgreSQL) instance in SQL page and click on the instance's name.

  3. In the Instance details page click on Import.

  4. In Cloud Storage file select the CSV file from bucket.

  5. In Format of import, select CSV

  6. In table write the name of the table you want to import the data in.

  7. Click on Import and the data will be imported.

I have tried the methods above myself and I uploaded successfully a custom CSV file to CloudSQL (PostgreSQL).

Upvotes: 2

Related Questions