Friday, March 15, 2013

Listing all column names in a PostgreSQL table

My PostgreSQL database contains a table with more than 100 columns, and I wanted to list the name and data type of each column, in a format that I could copy and paste. I was using Windows 7, but a similar technique should work on Linux. Here's one way to do it:
  • Open a DOS command window.
  • Change to PostgreSQL's bin directory. On my computer, the command is cd C:\Program Files\PostgreSQL\9.2\bin.
  • Make sure the directory C:\temp exists.
  • Execute this command: psql -U <your PostgreSQL user name> -d <your database name> -c "\d <your table name>" > c:\temp\out.txt
  • This will create the text file C:\temp\out.txt with a list of field names and data types.