Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

BigQuery EXTERNAL_QUERY to Postgres failing when accessing tables with or without schema name

We are receiving the following error when trying to query any of our tables in the postgres db.

Query:

SELECT * FROM EXTERNAL_QUERY("xxxx.us-east4.xxxxxxxx", "SELECT * FROM public.User;");

Error:

Invalid table-valued function EXTERNAL_QUERY Failed to get query schema from PostgreSQL server, prepare statement failed. Error: ERROR: relation "public.user" does not exist LINE 1: SELECT * FROM public.User; /* Federated query from BigQuery.... ^ at [1:15]

However, the following query does work:

Query public tables:

SELECT * FROM EXTERNAL_QUERY("xxxx.us-east4.xxxxxxxx", "SELECT * FROM pg_tables where schemaname = 'public';");
 
Results:
Screen Shot 2023-02-07 at 4.38.02 PM.png
 
Any hints, tips, tricks to try? 
 
Things we have done:
  • tried adding schema name, removing schema name, single quotes, double quotes,... all the formats
  • verified user has permission
  • verified schema is in search path
  • Query Insights is disabled
  • verified user can successfully query in DBeaver.
2 4 2,097
4 REPLIES 4

Hi mate, any luck with this? Got same problem.

same issue here

Hi,

I would like to confirm a few things:

  1. What database name and username combination did you use while creating the external connection - "xxxx.us-east4.xxxxxxxx ?

  2. Does the same user have privileges to read tables? If not, you can grant the necessary privileges using the following command:

GRANT SELECT ON ALL TABLES IN SCHEMA schema_name TO username;

Just let me know on above.

I had the same issue, but the solution is to add escaped quotes around the table name like this:
SELECT * FROM EXTERNAL_QUERY("xxxx.us-east4.xxxxxxxx""SELECT * FROM public.\"User\";");