Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
Postgresql return variable in type stored in a table

I'm currently developing an app and encountered a problem when trying to create a query on a table that uses a custom type.

Here is an example table and type,

CREATE TYPE address AS (
  street varchar(40),
  city   varchar(25),
  zip    varchar(5)
);
CREATE TABLE houses (
  id SERIAL PRIMARY KEY,
  address address
);

I need to retrieve a list with all the cities stored in the table houses. Here's what I tried to do:

SELECT address.city
FROM houses
GROUP BY address.city

It detects address.city as a table. Is there any way to do what I want to do in PostgreSQL?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I would not use a compound type like that in a table. But you can still do this, use parenthesis.

SELECT (address).city
FROM houses
GROUP BY (address).city

From the docs

To access a field of a composite column, one writes a dot and the field name, much like selecting a field from a table name. In fact, it's so much like selecting from a table name that you often have to use parentheses to keep from confusing the parser.

Instead, I would use a jsonb. Or, if you're going to use a compound type you should use stdaddr from PostGIS.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!