psycopg2 change schema does not work - postgresql

I am writing a simple python prog to connect and display results from Postgres table this is on AWS RDS. I have table mytest in public schema.
connection = psycopg2.connect(dbname='some_test',
user='user1',
host='localhost',
password='userpwd',
port=postgres_port)
cursor = connection.cursor()
cursor.execute("SET SEARCH_PATH TO public;")
cursor.execute("SELECT * FROM mytest;")
But this throws an error
psycopg2.ProgrammingError: relation "mytest" does not exist
LINE 1: SELECT * FROM mytest;
Connection is successful and I can query other basetables like
SELECT table_name FROM information_schema.tables
It is just that I cannot change to any other schema. I googled and tried all kinds of SET SERACH_PATH and commit it and recreate cursor etc. but no use. I cannot query any other schema.

ALTER USER username SET search_path = schema1,schema2;
After setting this the query works fine!

Related

In .NET 5 why am I unable to access PostgreSQL tables in a custom schema?

We have a custom schema (config) in our DB where we have multiple tables declared and populated. In DataGrip and pgAdmin alike we're able to see the tables in the custom schema. The following query shows us tables in both the public schema as well as in the custom schema:
select table_schema, table_name
from information_schema.tables
where not table_schema='pg_catalog'
and not table_schema='information_schema';
In DataGrip we had to "show all schemas"; in pgAdmin, everything was visible right away, tables from both schemas.
However, in .NET code, when we run the same query, we only see the public tables.
await using var conn = new NpgsqlConnection(connString);
await conn.OpenAsync();
await using var cmd = new NpgsqlCommand(
#"select table_schema, table_name
from information_schema.tables
where not table_schema = 'pg_catalog'
and not table_schema = 'information_schema';", conn);
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
Console.WriteLine(reader.GetString(0) + "." + reader.GetString(1));
The connection string does include: ;SearchPath=config,public
All we're getting back from that code is tables in public.
We noticed that there is a hostname for the cluster and a hostname for the DB instance. We were using one (I think it was the instance) and tried using the other (cluster hostname) and suddenly the .NET code was able to see the config schema tables.

Retrieve tables name from several databases in Postgresql?

I have two databases new_site,old_site I'm connecting to the database server via Postgres user and have full permission and I connect to new_site db.
I need to get tables names for old_site so I tried this:
SELECT table_name
FROM information_schema.tables
WHERE table_catalog = $$old_site$$;
but I get a null as result.
If I run this query:
SELECT table_name
FROM information_schema.tables
WHERE table_catalog = current_database();
I get back the table name and it works.
I expect the output is table name of old_site db, how can I do this?
I was also reading some solutions here like:
Selecting column name from other database table through function in PostgreSQL
But it's not like my case.

How to use a subquery as a database name in a DDL command?

I am wondering if it's possible to use the result of a subquery as database name in a PostgreSQL (9.5.1) DDL statement.
For example, I wanted to alter the current database with something like:
ALTER DATABASE (SELECT current_database()) SET a_var TO 'a_value';
If I run this, an error occurs:
ERROR: syntax error at or near "("
LINE 1: ALTER DATABASE (SELECT current_database()) SET ...
What's the correct way to use the sub-query (if possible)?
You need dynamic SQL for that:
DO
$do$
BEGIN
EXECUTE format($f$ALTER DATABASE %I SET x.a_var TO 'a_value'$f$, current_database());
END
$do$;
Using format() to escape the db name safely while being at it.
BTW, to unset:
ALTER DATABASE your_db RESET x.a_var;
To see the current setting:
SELECT current_setting('x.a_var');
(The DB default is not active before you start a new session.)
Related:
Table name as a PostgreSQL function parameter
Error when setting n_distinct using a plpgsql variable

No such table in ZSS Test Client window

Using the Test Client, I get a "No Such Table" error when trying to select some data from my database - sync was successful.
Here's my query: select * from schema.TableName.
Try with:
SELECT * FROM TableName
i.e. without the schema name in front
Or, if the table was from a schema other than dbo, it will be prefixed by the schema name + _ (see SQL Server Schemas in the ZSS Manager docs). So if the original table was MySchema.TableName, you'd use:
SELECT * from MySchema_TableName

ERROR: relation "stg_data_bt_sur" does not exist

I've created a table in postgresql which is OK and I'm able to do select/insert using SQL manager tool or Navicat Lite tool.
But, when I'm trying to make simple select from LINUX(ubuntu) I have following message:
postgres=# select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist
STATEMENT: select count(*) from stg_data_brest_surgery;
ERROR: relation "stg_data_brest_surgery" does not exist.
I also used table name with double quotes - same result.
Any idea what's the issue?
Chances are the schema isn't in your search path, Try \dn to list namespaces and then you can either add the schema like:
SELECT * from "schema"."table";
Or you can set your search path:
SET search_path="schema";
SELECT * FROM "table";
RESET search_path;