Appending the table inside a PSQL schema - postgresql

My goal is to append all the tables in the schema. Is there a way to loop through the table in one specific schema, and use those tables to append to each other to create a bigger table? (Here, all my tables have the same format for the columns).

Related

cannot truncate the tables in landing area after transfering data

I have 2 schemas with exactly the same 15 tables in Postgres. Data will be inserted in to first schema every 3 hours. then data needs to be transfer into second schema.
afterthat tables of the first schema needs to be truncated. (landing area needs to be empty).I wrote a trigger to transfer data after inserting to first schema into second schema.
but how tables of the first schema should be truncated?
I searched already and I tried two ways but non of them works.
1.I put the truncate command after all (insert into... conflict on... )in the same trigger's function that transfer data from first schema into second schema. which doesn't work.
2.I made another trigger which will implemented after insert into (or update) tables of second schema. which also doesn't work. why this one doesn't work? if data already inserted into second_schema. it should be possible to trucate first_schema. It isnot in use by active query.
Error->cannot TRUNCATE "table1" because it is being used by active queries in this session
what should I do? I need to tranfer data after every insert into first schema, to second schema. and then truncate all 15 tables of the first schema.
Tables of first schema should be empty for new insert.

synchronise rows of all postgres db tables using primary/foreign keys

I have a postgres database with 30 tables. The main table "general_data" has a column "building_code", which all the other tables use as foreign key.
What I would need is to synchronise the "building_code" columns in all tables, meaning if a row is added in "general_data" a row is created in ALL tables with the the same value in "building_code" (the other columns remain empty).
Is there an SQL function that does that?

How to convert multiple tables with same columns into one table in postgress

I have a Postgres database that carry over 200 tables with the same column names and datatypes respectively. I would like to join all of them into one table, how can I achieve this?
I have Postgres 9.4 and pgAdmin setup.
If the tables have identical column names and types, then you can create a parent table and arrange for all of the other tables to inherit from the parent table. After this, queries on the parent table will automatically query all of the child tables.
First create an empty parent table with the same definition as the 200 tables you already have.
Then, use ALTER TABLE on each of the 200 tables to make them inherit from the parent table.
CREATE TABLE myparenttable( LIKE mychildtable1 );
-- Repeat this for each of the child tables
ALTER TABLE mychildtable1 INHERIT myparenttable;
See also: Inheritance in the postgresql manual.

Can two temporary tables with the same name exist in separate queries

I was wondering, if it is possible to have two temp tables with the same name in two separate queries without them conflicting when called upon later in the queries.
Query 1: Create Temp Table Tmp1 as ...
Query 2: Create Temp Table Tmp1 as ...
Query 1: Do something with Tmp1 ...
I am wondering if postgresql distinguishes between those two tables, maybe through addressing them as Query1.Tmp1 and Query2.Tmp1
Each connection to the database gets its own special temporary schema name, and temp tables are created in that schema. So there will not be any conflict between concurrent queries from separate connections, even if the tables have the same names. https://dba.stackexchange.com/a/5237 for more info
The PostgreSQL docs for creating tables states:
Temporary tables exist in a special schema, so a schema name cannot be given when creating a temporary table.

postgresql: alter multiple columns

My database has severals table with some column type 'money'. I would like to alter all these columns (in different tables) in a single statement rather than change type column by column, to avoid omissions.
You'll have to repeat the altering query for every column.
You might want to create a program code to do that for you. You know, with loops.
In order for the database to alter all the tables atomically you should enclose all the altering queries in a transaction (PostgreSQL supports transactional DDL).