Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud SQL - PostgreSQL - how to import a .csv file?

I have a Cloud SQL instance running PostgreSQL, and a .csv file on my local computer.

I created a user and a database. I logged in as that user, with that database and created a table that matches my .csv file.

I have a .csv file, and an import.sql file with the following:

COPY <my-table-name>
FROM 'C:\<path-to>\data.csv'
WITH (FORMAT csv);

When I run the psql command:

psql -f import.sql <connection string>

I get back:

psql:./import.sql:3: ERROR:  must be superuser to COPY to or from a file
HINT:  Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.

I tried making my user a 'superuser' by doing it as the postgres user, but I get this:

psql:./add-superuser.sql:1: ERROR:  must be superuser to alter superusers

Showing all users, shows:

postgres-> \du
 cloudsqladmin     | Superuser, Create role, Create DB, Replication, Bypass RLS 
 cloudsqlagent     | Create role, Create DB                                     | {cloudsqlsuperuser}
 cloudsqlreplica   | Replication                                                | {}
 cloudsqlsuperuser | Create role, Create DB                                     | {}
 <my-user>         | Create role, Create DB                                     | {cloudsqlsuperuser}
 postgres          | Create role, Create DB                                     | {cloudsqlsuperuser}

postgres is a cloudsqlsuperuser, but not a 'Superuser'. How to I import this .csv file?

I don't know how to login/get access as the user cloudsqladmin, or to grant a role to allow importing a file. I desperately need help quickly.

Google's documentation in this one area of Cloud Sql, with Postgres is horrible. It sends me all over, and is very confusing.

like image 446
user10664542 Avatar asked Feb 03 '26 11:02

user10664542


2 Answers

PostgreSQL is a managed service, therefore you have some limitations regarding the users. You can't grant SUPERUSER permissions. What you can do so far, is to import the CSV file from the console.

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).

like image 115
Andrei Cusnir Avatar answered Feb 06 '26 11:02

Andrei Cusnir


got exactly the same problem yesterday. I needed to execute the following command :

COPY <my-table-name>
FROM STDIN
WITH (FORMAT csv);

and then copy-paste the content of my file in the console. Worked as a charm. Juste be sure to end your file by \. to close the input

like image 26
Teocali Avatar answered Feb 06 '26 11:02

Teocali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!