Can we create hidden column in the table. It should be listed only when I specify column name explicitly in select statement.
2 Answers
You can effectively do this by creating a view and selecting only the columns you wish to show.
6 Comments
GRANT the SELECT right on the base table. Look into security_barrier views. 9.4 adds support for simply updatable security barrier views; in older versions you can use rule- or trigger-based updatable views. Or if you don't mind the potential information leakage via malicious predicates, just use an ordinary view - if a user has the right to select from a view, they implicitly gain the right to select the columns of the underlying table via the view, so you don't have to explicitly GRANT access to the table.No, there is no supported and safe way to create a hidden column that's listed only when you specify the column name explicitly. PostgreSQL doesn't provide any user-accessible way to hide user-defined columns from the * wildcard.
You could use any user interface layer / query builder of your choice to do this, though.
(PostgreSQL actually does have hidden columns, as you'll see if you select ctid,xmin,xmax from some_table, but it doesn't allow users to add to the set of hidden columns. It is possible to modify the system catalogs directly to trick PostgreSQl into thinking that a user defined column is a hidden system column, but it's a really bad idea to mess directly with the catalogs, so I won't explain how in detail. If you insist on doing this, read the documentation on pg_attribute ... but understand that you're creating a giant foot gun.).
You can set column permissions so a user can only select some columns, though again you can't say "all except this one", you have to say "I want them to be able to see these ones".
Update: @maybeWeCouldStealAVan has the most sensible suggestion: you probably want a view. Mark that answer as correct, not mine.
SELECT *today and application B wants to add a new field, application A would most likely fail because it gets an additional column back it doesn't expect. The app shouldn't do aSELECT *in the first place but such things happen and when you don't have control over the code, such as with a third-party application, you get a lot of problems and then even views can't help. docs.oracle.com/database/122/ADMIN/…