How can I get a tablename of a PostgreSQL cursor? - postgresql

I know that in Oracle we can do something like select * from TABLE(DBMS_XPLAN.DISPLAY_CURSOR('$$SQL_ID')). But there is no sql id field in pg_cursors catalog. We have only declaration text. And in general it's not Oracle. So, are there any ways to get a table name of an opened cursor in postgreSQL, besides parsing a sql statement(pg_cursors.statement)?

Related

Copy data schema to clipboard in Tableau

Through Amazon Aurora, I've got connection to hundreds of unknown tables with different schema. I need to figure out which one of them has got a field (or column name) that contains a particular text as substring, say freq. Loading these tables one by one into Tableau Worksheet and then manually checking their schema is proving extremely cumbersome task and error-prone.
Is there a way to get the schema of all these tables into some textual form like Tablename : Colm1, Colm2, ..., ColmN?
or, is it possible to right click on a table in Tableau and get Copy Schema to Clipboard option?
I wouldn't use Tableau for this. Try this from your favorite query tool:
SELECT table_name, column_name
FROM information_schema.columns
WHERE column_name like '%freq%'

create (or copy) table schema using postgres_fdw or dblink

I have many tables in different databases and want to bring them to a database.
It seems like I have to create foreign table in the database (where I want to merge them all) with schemas of all the tables.
I am sure, there is a way to automate this (by the way, I am going to use psql command) but I do not know where to start.
what I have found so far is I can use
select * from information_schema.columns
where table_schema = 'public' and table_name = 'mytable'
I added more detail explanation.
I wanted to copy tables from another database
the tables have same column names and data type
using postgres_fdw, I needed to set up a field name and data type for each tables (the table names are also same)
then, I want to union the tables have same name all to have one single table.
for that, I am going to add prefix on table
for instance, mytable in db1, mytable in db2, mytable in db3 as in
db1_mytable, db2_mytable, db3_mytable in my local database.
Thanks to Albe's comment, I managed it and now I need to figure out doing 4th step using psql command.

temp tables in postgresql

I'm coming from a background in SQL Server where I would create temp tables using the:
select id
into #test
from table A
I've just moved into a PostGresql environment and I was hoping I could do the same, but I'm getting a syntax error. I did a search and it seems like you have to do a Create Table statement.
Is it not possible to easily create temp tables in Postgres?
Postgres supports SELECT INTO, so this should work fine:
SELECT id
INTO TEMP TABLE test
FROM a
You can also use CREATE TABLE AS:
CREATE TEMP TABLE test AS
SELECT id FROM a
This version is generally preferred, as the CREATE statement provides additional options, and can also be used in PL/pgSQL functions (where the SELECT INTO syntax has been hijacked for variable assignment).

Why CREATE/ALTER schema with current date cannot accept any space or symbol using POSTGRESQL?

Why CREATE or ALTER schema cannot accept any space or symbol using POSTGRESQL? I kept getting error msg for creating schema with current date.
This will give you schemaname with current date
select 'test_' || to_char(CURRENT_DATE, 'MM_DD_YYYY') AS SCHEMA_NAME;
but if you want to use it for ALTER RENAME TO, it wont accept. I've tried several ways. It seem it wont work. Anyone face this before?
I try to use this script:
ALTER SCHEMA test RENAME TO 'test_'|| to_char(CURRENT_DATE, 'MM_DD_YYYY') AS SCHEMA_NAME';

How to include schema inf while using dblink in PostgreSQL?

All
I am trying to use dblink in PostgreSQL to run query on different databases. The following works if the table "user" is under the public schema:
select * from dblink(
'hostaddr=1.2.3.4 port=5434 dbname=dbname user=username password=password',
'select id from user')
as t1(
id bigint
);
However, I need to run the query on some other defined schemas. Does anyone know how to add the schema information in the above query? I can't figure it out from the PostgreSQL docs.
When you write SQL query like
SELECT id FROM user
PostgreSQL will resolve table name like user into fully qualified name like schema.tablename using schema search path, which by default is set to "$user",public.
In other words, user will resolve into public.user unless you tweaked server configuration.
However, you can specify schema explicitly in your statement, like this:
SELECT id FROM otherschema.user