18

There are many useful shortcuts in psql console like \d or \l.
I wondering is there one for SELECT * FROM table_name?
I use this query often for learning purposes so it would be quite helpful not to have to write select * from ... all the time.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
user3616181
  • 975
  • 2
  • 8
  • 27
  • You shouldn't write `SELECT *` at all :-) This is usually considered bad practice (for production queries, of course you can do it for testing): You can't see which columns will be returned and in which order, you usually don't really need all columns and when the table definition is altered this will change the layout of the returned data. – dnoeth May 16 '15 at 13:07
  • 11
    @dnoeth: There are plenty of cases where you need `SELECT *` and even more where it's just convenient. The fact that it's often (ab)used in places where it shouldn't, does not warrant a general statement like "You shouldn't use it". That like saying: "You shouldn't use knifes". – Erwin Brandstetter May 16 '15 at 13:11
  • @dnoeth I know what you mean but as I said "for learning purposes" ;) – user3616181 May 16 '15 at 13:12
  • This is why I prefer GUI clients ;) I just highlight the tablename in the editor, press a shortcut and the tool runs a `select *` for me. –  May 16 '15 at 13:43
  • GUI is a dream. I have only text mode available because course form requires it :) – user3616181 May 16 '15 at 16:06

1 Answers1

41

There is a shortcut for SELECT * FROM in standard SQL, so you can use it in psql:

TABLE tablename;

This syntax shorthand can only be used with a limited range of clauses. The manual:

It can be used as a top-level command or as a space-saving syntax variant in parts of complex queries. Only the WITH, UNION, INTERSECT, EXCEPT, ORDER BY, LIMIT, OFFSET, FETCH and FOR locking clauses can be used with TABLE; the WHERE clause and any form of aggregation cannot be used.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228