Search code examples
rdatabaserpostgresql

How to get the column names from Database in R?


How to get the only particular table column names using R?

Sample code:

df<-dbgetQuery(con,"select * from table 1 limit 100")
colnames(df)

Is there any alternative for the above query?


Solution

  • got the solution and will get the colnames using the below query.

    dbGetQuery(con,"SELECT column_name
    + FROM information_schema.columns
    + WHERE table_schema = 'your schema'
    +   AND table_name   = 'table name'") ##ORDER  BY ordinal_position; to orderby
    

    sample query :

    dbGetQuery(con,"SELECT column_name, data_type
    + FROM   information_schema.columns
    + WHERE  table_name = 'data 1'
    + ORDER  BY ordinal_position")
    

    Both query's are working good.