'How to use pg_restore to import dump file in cloudsql?

I want to import the .sql file created with pg_dump into cloudsql.

The console and gcloud commands are both custom-type dump files, so they are told to use pg_restore.

On my local machine

pg_restore -h cloud_sql_ip -U postgres -d database sqlfile.sql

When I use the command, I get the following error:

pg_restore: error: connection to database "database" failed: could not connect to server: Operation timed out
        Is the server running on host "cloud_sql_ip" and accepting
        TCP/IP connections on port 5432?

If I use the gcloud sql connect command, it will connect without any problems.

I don't know why I am getting the error.

Thank you for your help.



Solution 1:[1]

PostgreSQL's pg_restore -d from your local (On my local machine) requires you to first authorize your public IP, there by allowing your machine to connect to your cloudsql instance...

Per GCP

You have not authorised any external networks to connect to your Cloud SQL instance.

If you click on the Connections link on the left nav, under the Networking tab, you will see the above message...you will need to add your pubic IP (click Add Network)

enter image description here

Hopefully this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Didier Jean Charles