Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get column names of a table

Member
 
Join Date: Jun 2007
Location: CHENNAI
Posts: 54
#1: Aug 14 '07
hi all,

How to get fieldnames of a table using postgresql?

Can someone help me to overcome this issue?

Like if i have table called "employee" i need to fetch the fieldnames(id,name,workstatus..etc) of employees alone not its records...Is any method to get this?


Thanks in advance...

Moderator
 
Join Date: Nov 2006
Location: Boston, USA
Posts: 505
#2: Aug 14 '07

re: how to get column names of a table


If it does not have to be a query the simplest approach is to use the metacommand \d in psql session:
Expand|Select|Wrap|Line Numbers
  1. postgres=# \d bar
  2.                                 Table "public.bar"
  3.  Column |         Type          |                    Modifiers
  4. --------+-----------------------+--------------------------------------------------
  5.  id     | integer               | not null default nextval('bar_id_seq'::regclass)
  6.  f1     | character varying(32) |
  7.  f2     | character varying(32) |
  8. Indexes:
  9.     "bar_pkey" PRIMARY KEY, btree (id)
  10. Triggers:
  11.     trig_audit BEFORE INSERT OR UPDATE ON bar FOR EACH ROW EXECUTE PROCEDURE audit_proc()
  12.  
\d without a table name will give you a list of all user tables in the database.
See psql manual for details
Member
 
Join Date: Jun 2007
Location: CHENNAI
Posts: 54
#3: Aug 14 '07

re: how to get column names of a table


Thanks michael for your help..

I need a query to fetch the fieldnames is any method available for that?

If so guide me the solution or its related pages...

Regards..

Barani
Moderator
 
Join Date: Nov 2006
Location: Boston, USA
Posts: 505
#4: Aug 14 '07

re: how to get column names of a table


I am pretty sure this question came up before, I remember I posted the sql query that gives this info few months ago.
Try to search the Postgresql forum, if you found the answer post a link to it - it may help others.
Meanwhile take a look at the Postgresql Information Schema
Member
 
Join Date: Jun 2007
Location: CHENNAI
Posts: 54
#5: Aug 14 '07

re: how to get column names of a table


Hi michael,

I have gone through your guidance and searched perl forum...

You have mentioned the query as "select count(*) from tablename"

Its not giving me the fieldnames of a table...

Kindly suggest me some other way...

Thanks&Regards,

Barani..
Moderator
 
Join Date: Nov 2006
Location: Boston, USA
Posts: 505
#6: Aug 14 '07

re: how to get column names of a table


Quote:

Originally Posted by Lastknight

Hi michael,
I have gone through your guidance and searched perl forum...

You have mentioned the query as "select count(*) from tablename"
Its not giving me the fieldnames of a table...

No, no I said:
>> Try to search the Postgresql forum ...

"select count(*) from tablename" is not what you need, this query only returns the number of rows in the table.

I looked at the old postings, but could not find anything, which is strange, I remember replying to the same question few months ago.
I'll try to dig it out for you.
Moderator
 
Join Date: Nov 2006
Location: Boston, USA
Posts: 505
#7: Aug 15 '07

re: how to get column names of a table


This works for me:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     a.attname as "Column",
  3.     pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
  4. FROM
  5.     pg_catalog.pg_attribute a
  6. WHERE
  7.     a.attnum > 0
  8.     AND NOT a.attisdropped
  9.     AND a.attrelid = (
  10.         SELECT c.oid
  11.         FROM pg_catalog.pg_class c
  12.             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  13.         WHERE c.relname ~ '^(foo)$'
  14.             AND pg_catalog.pg_table_is_visible(c.oid)
  15.     )
  16. ;
  17.  
Replace foo on line 13 with the name of your table.
The code should produce the output similar to this:
Expand|Select|Wrap|Line Numbers
  1.    Column  |       Datatype
  2. --------+-----------------------
  3.  id     | integer
  4.  fs1    | character varying(32)
  5.  fi2    | integer
  6.  fn3    | numeric(5,0)
  7. (4 rows)
  8.  
If you really need this functionality consider making a function that would take the table name as an argument and spit out the result.
Member
 
Join Date: Jun 2007
Location: CHENNAI
Posts: 54
#8: Aug 16 '07

re: how to get column names of a table


Quote:

Originally Posted by michaelb

This works for me:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     a.attname as "Column",
  3.     pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"
  4. FROM
  5.     pg_catalog.pg_attribute a
  6. WHERE
  7.     a.attnum > 0
  8.     AND NOT a.attisdropped
  9.     AND a.attrelid = (
  10.         SELECT c.oid
  11.         FROM pg_catalog.pg_class c
  12.             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  13.         WHERE c.relname ~ '^(foo)$'
  14.             AND pg_catalog.pg_table_is_visible(c.oid)
  15.     )
  16. ;
  17.  
Replace foo on line 13 with the name of your table.
The code should produce the output similar to this:
Expand|Select|Wrap|Line Numbers
  1.    Column  |       Datatype
  2. --------+-----------------------
  3.  id     | integer
  4.  fs1    | character varying(32)
  5.  fi2    | integer
  6.  fn3    | numeric(5,0)
  7. (4 rows)
  8.  
If you really need this functionality consider making a function that would take the table name as an argument and spit out the result.

Hi michael,

Thanks for your help and cooperation towards my query...

Its working for me now...i have searched in the postgresql forum but i have not found this type of query over there ...

Any way i like to thank you once again for your timely help...

Hope we will have furhermore discussions..

Regards,
Barani...
Member
 
Join Date: Jun 2007
Location: CHENNAI
Posts: 54
#9: Aug 16 '07

re: how to get column names of a table


Hi michael,

The query you have mentioned me is working and giving me the result what i needed...

But i need to understand its structure, in the previous reply u have mentioned about information schema...

Is that is related with the query you have posted...


Kindly post me the details...

With regards,

Barani...
Reply