0

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.

Schema: codes

Table Name: watch_list

Here are the column_names in my table:

watch_list_id, watch_name, watch_description

I tried what I found in the web:

SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
  AND table_name   = 'watch_list'

It output is not what I wanted. It should be:

watch_list_id, watch_name, watch_description

How to do this?

6
  • 1
    what was the output you got? Commented Jun 1, 2015 at 6:31
  • table_catalog, table_schema, table_name, column_name, ordinal_position. . . Commented Jun 1, 2015 at 6:32
  • in the result the column_name have the name of the columns in the table.. Commented Jun 1, 2015 at 6:34
  • are you saying you want the name of all columns in one row? Commented Jun 1, 2015 at 6:35
  • Oops. Thanks guys. I already got it. Commented Jun 1, 2015 at 6:38

2 Answers 2

3

If you want all column names in a single row, you need to aggregate those names:

SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
  AND table_name   = 'watch_list'
GROUP BY table_name;

If you remove the condition on the table name, you get this for all tables in that schema.

Sign up to request clarification or add additional context in comments.

Comments

-1
SELECT table_name FROM information_schema.tables WHERE table_schema='public'

3 Comments

That will display all tables in a schema. The OP wants all columns for one table.
This will help you: SELECT column_name FROM information_schema.columns WHERE table_name = 'watch_list'
Which is essentially the same query as already shown in the question and it will not show the columns in a single row, but will show one row in the result for each column

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.