I have a slightly more complicated join:
SELECT person.given_name, person.family_name, person.age, person.sex, person.state,
person.zip, person.street, person.residential_number,
c1.name as citizen_of_country,
c2.name as hospitalization_country,
c3.name as infection_country
FROM patients
JOIN person on patients.person_id = person.id
JOIN country c1 on person."citizenOf_country_id" = c1.id
JOIN country c2 on patients.hospitalized_in_country_id = c2.id
JOIN country c3 on patients.infected_in_country_id = c3.id;
.. and I'd like to somehow get the name of all columns in this new table. Based on some answers I found, I tried
SELECT DISTINCT column_name FROM information_schema.columns WHERE table_schema = 'public' AND [code];
Where [code] is the block above.. But it didn't work, giving the error "Subquery must return only one column". I'm new to databases so I'm not sure how to handle this correctly.
Best Answer
You can get the information from yourcursor.description. Example:
You can find more info under metadata on zetcode.com/python/psycopg2/