Home > Net >  inserting results of different queries of different tables into a new created table in Postgres via
inserting results of different queries of different tables into a new created table in Postgres via

Time:11-27

So i have created a table with multiple columns to collect some information about a database

CREATE TABLE DATENBANKEN (

    ID serial,
    name VARCHAR(20),
    Erstellt timestamp,
    Datenbankgröße VARCHAR(20),
    "Collation" VARCHAR (20),
    Beschreibung VARCHAR (50) 
)

and with the following insert statement i was able to fill the rows with the desired information

INSERT INTO DATENBANKEN (id, name, Erstellt, Datenbankgröße, "Collation")
    SELECT pg_database.oid,
    pg_database.datname,
    (pg_stat_file('base/'||pg_database.oid ||'/PG_VERSION')).modification,
    pg_size_pretty(pg_database_size(datname)),
    pg_database.datcollate datcollate from pg_database

this is the results

all the values above were captured from on table (pg_database)

now the last value "Beschreibung" is located in another table named (pg_shdescription)

so in this case i had to make another insert statement specifically for the column "Beschreibung"

INSERT INTO DATENBANKEN (Beschreibung)
    select pg_shdescription.description from pg_shdescription

as you can see the rows in the column "Beschreibung" were not inserted beside the first three rows as i expected, but were added as additional rows with no connection to the data above.

this is the table pg_shdescription and as you can see, for every objoid there is a specific description. So 1 is "default template for new databases"

so here the 4th row in the column "Beschreibung" should have been in the second row where the datacenter name "template 1 is"

what did i do wrong here or what is the best way to insert certain data from different tables into a new table that are still linked together?

I really appreciate your help, any help

  • Related