Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:33:56+00:00 2026-05-31T18:33:56+00:00

I have a server with PostgreSQL 8.4 which is being rebooted every night at

  • 0

I have a server with PostgreSQL 8.4 which is being rebooted every night at 01:00 (don’t ask) and need to get a list of connected users (i.e. their timestamps are u.login > u.logout):

SELECT u.login, u.id, u.first_name
FROM pref_users u
WHERE u.login > u.logout and 
      u.login > now() - interval '24 hour'
ORDER BY u.login;

           login            |           id   | first_name
----------------------------+----------------+-------------
 2012-03-14 09:27:33.41645  | OK171511218029 | Alice
 2012-03-14 09:51:46.387244 | OK448670789462 | Bob
 2012-03-14 09:52:36.738625 | OK5088512947   | Sergej

But comparing u.login > now()-interval '24 hour' also delivers the users before the last 01:00, which is bad, esp. in the mornings.

Is there any efficient way to get the logins since the last 01:00 without doing string acrobatics with to_char()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:33:57+00:00Added an answer on May 31, 2026 at 6:33 pm

    This should be 1) correct and 2) as fast as possible:

    SELECT u.login, u.id, u.first_name
    FROM   pref_users u
    WHERE  u.login >= now()::date + interval '1h'
    AND    u.login > u.logout
    ORDER  BY u.login;
    

    As there are no future timestamps in your table (I assume), you need no upper bound.

    Some equivalent expressions:

    SELECT localtimestamp::date     + interval '1h'
         , current_date             + interval '1h'
         , date_trunc('day', now()) + interval '1h'
         , now()::date              + interval '1h'
    

    now()::date used to perform slightly faster than CURRENT_DATE in older versions, but that’s not true any more in modern Postgres. But either is still faster than LOCALTIMESTAMP in Postgres 14 for some reason.

    date_trunc('day', now()) + interval '1h' slightly differs in that it returns timestamptz. But it is coerced to timestamp according to the timezone setting of the current session in comparison to the timestamp column login, doing effectively the same.

    See:

    • Ignoring time zones altogether in Rails and PostgreSQL

    To return rows for the previous day instead of returning nothing when issued between 00:00 and 01:00 local time, use instead:

    WHERE  u.login >= (LOCALTIMESTAMP - interval '1h')::date + interval '1h'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PostgreSQL server and I need to run some code whenever an
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
I have a Tomcat and PostgreSQL installed on a server. I'm having a connection
I have a client-server Silverlight application, which is use Socets. I have server appliaction
I just downloaded jasperreports server (community edition) which uses postgresql. (there is no option
I have to optimize queries by tuning basic PostgreSQL server configuration parameters. In documentation
I have some backend functionality which need to be written in C# (for political,
Which optimizations do you have in mind for a Linux, Apache, PHP & PostGreSQL
I have database schema for an integration project in which I need to be
I have an application server which connects to a database server. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.