473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to get column names of a table

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...
Aug 14 '07 #1
9 46598
michaelb
534 Expert 512MB
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
Aug 14 '07 #2
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
Aug 14 '07 #3
michaelb
534 Expert 512MB
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
Aug 14 '07 #4
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..
Aug 14 '07 #5
michaelb
534 Expert 512MB
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.
Aug 14 '07 #6
michaelb
534 Expert 512MB
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.
Aug 15 '07 #7
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...
Aug 16 '07 #8
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...
Aug 16 '07 #9
I did not find any such query. Your query works for me.
Oct 19 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bart Van der Donck | last post by:
Hello, I am using MySQL 4.0. Say that I have a table named "mytable" having a column "ID" and a column "test columnname". When dumping: mysqldump --opt DATABASE -uUSER -hHOST -pPASS >...
2
by: Matthew Cascio | last post by:
My understanding is that using reserved words as column names is allowable as long as they are quoted. I am trying to create a table dynamically with columns defined by the first row of a text...
3
by: Tyler Hudson | last post by:
/*Code below raises following errors: Server: Msg 245, Level 16, State 1, Line 6 Syntax error converting the varchar value 'a' to a column of data type int. */ create table #x (i integer, c...
1
by: noor | last post by:
Hi I have been working since 2 days to device a method to export sql table into csv format. I have tried using bcp with format option to keep the column names but I'm unable to transfer the file...
4
by: John.Arthur | last post by:
Hi, I am learning Ms Sql and I found that a lot of the tables I am finding ( in different tutorials ) are using special names for their column names such like au_username, au_salery ... Is the...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
1
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
4
by: natG | last post by:
Hi; I am transferring data from MySql to db2 using my own java/jdbc program. Working out ok, except for the fact that our apps use mixed-case names for tables and columns. Although my CREATE TABLE...
1
by: christianlott1 | last post by:
I want to provide users with an interface to create a custom merge (all in Access, not Word). User will put in a set of brackets ("<>") in a memo field and when they click the merge button it will...
3
by: beautifulcarcass | last post by:
Hi, in this school project im making, im having a problem if i could display the column names from a table on a MYSQL database to a webpage through PHP with a loop is there a function to display...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.