1

When you fire up rails dbconsole you can easily execute commands such as:

select * from users limit 1;

However some commands require using Postgres functions such as now() or timezone(zone, timestamp)

How can you evaluate Postgres functions without needing to select data?

It would be nice to be able to play with some of these functions without having to wrap them into a select statement.

=> now()
# now | 2013-11-18 22:32:48.814876+00

How can I do this directly from dbconsole?

Community
  • 1
  • 1
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
  • @zeantsoi's answer is correct, `rails dbconsole` (with a mysql database) is the equivalent of running `mysql -u username -ppassword database_name`, it's a raw connection. – David Nov 18 '13 at 23:44

1 Answers1

4

The Rails database console is purely a way to interface with the specified database's command line. It doesn't provide any Rails wrappers or convenience methods – you're limited to whatever the database language provides. In PostgreSQL, the correct query to retrieve the current timestamp with timezone is as follows:

SELECT now();
Peter Nixey
  • 16,187
  • 14
  • 79
  • 133
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • Ahah - works a treat. Out of interest do you know how to get that time local to a particular timezone: http://stackoverflow.com/questions/20059342/whats-the-cleanest-way-to-get-the-current-local-time-in-postgres/20060576?noredirect=1#20060576 ? Thank you. – Peter Nixey Nov 19 '13 at 00:12
  • 1
    I've added an [answer](http://stackoverflow.com/a/20060576/1086529), but I'm not sure that it substantially differs from what you've already suggested. Am I missing something? – zeantsoi Nov 19 '13 at 00:27