PostgreSQL - Incorrect time - postgresql

I have a database that is deployed in GCP. I connect to DB via the GCP console and use Dbeaver to run queries. When I run SELECT NOW(); it's showing the time at which I started the session and not the current time.

Looks like you started a TRANSACTION and NOW() shows you the time the transaction started. You have 2 option to solve this issue:
Don't use a transaction and you can work with NOW()
Or inside a transaction, use CLOCK_TIMESTAMP()
See also the manual about this behaviour.

Related

Monitor postgres 9 queries

There is a postgres 9 with database AAA that is used by my application. I use pgadmin 4 to manage it manually.
I would like to check what queries are executed by this application on database AAA in real time.
I did a research about monitoring options in pgadmin in vain.
Is is possible to do that by using just pgadmin4? Or is it necessary to use another tool (if yes - what is he name of this tool)?
When I point pgAdmin4 at a 9.6 server, I see a dashboard by default which shows every session (done by querying pg_stat_activity). You can then drill down in a session to see the query. If a query last for less time than the monitoring interval, then you might not see it if the sample is taken at the wrong time.
If that isn't acceptable, then you should probably use a logging solution (like log_statemnt='all') or maybe the pg_stat_statements extension, rather than sample-based monitoring. pg_stat_statements doesn't integrate with the dashboard in pgAdmin4, but you can select from the view from an SQL window just like you can run any other SQL. I don't believe pgAdmin4 offers a built-in way to monitor the database server's log files, the way pgAdmin3 did.

How to set timezone on postgresql jdbc connection created by flyway?

I have a postgresql database that I deploy scripts to using flyway. I use the maven flyway plugin to launch the database build against the target database. In that build I have scripts that do things like:
create table my_table(
my_date_time timestamp with time zone not null
);
insert into my_table(my_date_time)
select '2000-01-01';
The postgresql database timezone is set to UTC. My client machine (that runs the maven/flyway build) is running CEST (UTC+2:00).
When the scripts run, the database interprets the string literal above as '1999-12-31 22:00:00+00:00' and writes that to storage.
It seems that the jdbc client connection created by flyway is not setting its client timezone to UTC, but is taking the timstamp string and interpreting it as '2000-01-01 00:00:00+02:00'.
How can I set the client timezone on the jdbc connection created by flyway to UTC? Is there a setting that can be placed in my flyway.conf file?
If I change my local machine timezone to UTC the problem goes away, but I'd rather not do that on my development workstation.
A colleague suggested the following
mvn -Duser.timezone=UTC flyway:migrate
Instead of just giving it a date, which postgres has to interpret, it would be better to fully specify the timestamp:
insert into my_table(my_date_time)
select '2000-01-01T00:00:00+00';
If that is not possible, you could try running mvn with -Duser.timezone=UTC but fully specifying the timestamp is the better option as it leaves no room for misinterpretation or misconfiguration.
Another way is to set session timezone in migration script itself
SET timezone TO 'UTC';
i use flyway CLI in a script. so setting correct timezone to the corresponding environment variable before executing flyway command works for me:
export TZ=GMT

How do I set Google Cloud SQL - PostgreSQL timezone to UTC

I see Cloud SQL - MySQL has a Database Flag to set the default_time_zone. I cannot however, find how to do the same for Cloud SQL - PostgreSQL. I need my time zone to be UTC.
The flags that you see in Cloud SQL - MySQL in Configuring Database Flags documentation are provided by MySQL. If you click on the flag default_time_zone that you are referring to, then you will see that you will be redirected to MySQL documentation page. For the Cloud SQL - PostgreSQL the flags that you see in the Configuring Database Flags documentation, are provided by PostgreSQL, therefore they don't expose this flag or any similar flag like this.
To change the timezone of the PostgreSQL instance, you have to SSH into it and do it by executing query:
Go to the SQL page on the console and find your PostgreSQL instance.
On the instance's details record you will find three dots at the end. Click and select the option Connect VM. On the right side of your window you will see a tutorial.
Follow the tutorial steps to set up a Compute Engine VM Instance that will be connecting to your PostgreSQL.
After you finish all the steps form the tutorial. Go to the Compute Engine page and SSH to the instance that you created for connecting to the PostgreSQL.
Execute the psql "sslmode=disable dbname=postgres user=postgres hostaddr=[POSTGRESQL_PUBLIC_IP]" to connect to the instance.
Execute the SELECT NOW(); to see the current timezone, that your PostgreSQL is running at.
Execute the SET timezone to 'UTC'; to set the timezone of the PostgreSQL to UTC.
Execute the SELECT NOW(); to verify that the default timezone server updated successfully.
You can also connect to the PostgreSQL from Cloud Shell and skip the part of the VM Instance creation.

Postgres time on heroku 5 minutes ahead

It's currently 11:05 UTC. When I call "SELECT CURRENT_TIMESTAMP" on our Heroku Postgres DB it says 11:10 UTC.
How is that massive difference possible? Do we have to manually set the current time?
current_timestamp function relies on system time and returns time of the beginning of transaction (if any). in your case probably system time is not sync with ntp servers. you can check system time with command 'date'.
Heroku said the NTP client on this machine stopped working. They've restarted it and it's working again.

connection time zone issue with jOra eclipse plugin

I started using the jOra eclipse plugin. The plugin seems pretty robust and I'm hoping to stop using SQLDeveloper for 95% of my database needs.
Many of our tables have columns of type TIMESTAMP with LOCAL TIME ZONE. I can connect to the oracle DB using a jdbc string and the plugin seems to function very well. However, when I try to update one of these TIMESTAMP with LOCAL TIME ZONE values, I get a sql exception: java.sql.SQLException: connection session time zone was not set.
Does anyone know how I can set the time zone through the jdbc connection url? jOra doesn't seem to support adding custom connection properties, so the connection URL is really my only option.
Update: Running version 1.0.1, which I believe is the latest version.
Update2: Apparently I can perform an update statement in the sql worksheet just fine, just can't use their detail browser interface to update.
What version do you use? According to their release notes this issue was already fixed in 0.9.0. Consider upgrading. If still in vain, I'd report a bug over there, they seem to maintain it well enough.
After you connect to the DB, try running:
ALTER SESSION SET time_zone='+01:00';
Alternatively you could create a system trigger:
CREATE OR REPLACE TRIGGER setSessionTZ
AFTER LOGON ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET time_zone=''+01:00''';
END;