0

I'm trying to run custom SQL in my migration. This is how it looks like:

from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.RunSQL(
            "SET timezone TO 'UTC'",
        ),
    ]

When I run it with

./manage.py migrate helper

I can see in the logs that SQL command was run:

[17/Feb/2017 20:37:37] DEBUG [django.db.backends.schema:103] SET timezone TO 'UTC'; (params None)
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) SET timezone TO 'UTC';

But when I go and check it in database it still says timezone is localtime:

gdp=# show timezone;
 TimeZone  
-----------
 localtime
(1 row)

If I run the same command manually it works OK:

gdp=# SET timezone TO 'UTC';
SET
gdp=# 
gdp=# show timezone;
 TimeZone 
----------
 UTC
(1 row)

Django version: 1.10.5

PostgreSQL version: 9.5.5

Full logs:

[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.591) CREATE EXTENSION IF NOT EXISTS postgis; args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.002) 
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends.schema:103] CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMARY KEY, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" timestamp with time zone NOT NULL); (params None)
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.014) CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMARY KEY, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" timestamp with time zone NOT NULL); args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.001) 
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
[17/Feb/2017 20:37:37] DEBUG [django.db.backends.schema:103] SET timezone TO 'UTC'; (params None)
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) SET timezone TO 'UTC'; args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) 
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) INSERT INTO "django_migrations" ("app", "name", "applied") VALUES ('helper', '0001_initial', '2017-02-17T20:37:37.272476+00:00'::timestamptz) RETURNING "django_migrations"."id"; args=(u'helper', u'0001_initial', datetime.datetime(2017, 2, 17, 20, 37, 37, 272476, tzinfo=<UTC>))
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.001) 
            SELECT c.relname, c.relkind
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid); args=None
[17/Feb/2017 20:37:37] DEBUG [django.db.backends:90] (0.000) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()

EDIT:

I have these settings:

USE_TZ = True 
TIME_ZONE = 'UTC' 

but with this Django saves datetime to Postgres like this '2016-09-12T08:06:45-04:00' using my local timezone. It does convert it back to UTC when I query database through Django but I'd like to have 'clean' datetime in Postgres that is something like this '2016-09-12T12:06:45+00'.

2 Answers 2

1

I found an answer to my question in PostgreSQL documentation 8.5.3. Time Zones:

The SQL command SET TIME ZONE sets the time zone for the session.

I was able to solve this by setting timezone in postgresql.conf (for me it was located in /etc/postgresql/9.5/main).

Sign up to request clarification or add additional context in comments.

Comments

0

Django sets its own timezone when you use a DateTimeField. In your settings.py, you should find this setting:

TIME_ZONE='UTC'

You can disable djangos timezone-usage by setting this to None. However, I believe that would simply convert all DateTimeField-values into values without a timezone. (didn't try it)

I recommend using djangos TIME_ZONE setting in any case, since it ensures consistency over all databases.

1 Comment

Forgot to mention this (will edit my post): I have these settings: USE_TZ = True TIME_ZONE = 'UTC' but with this Django saves datetime to Postgres like this '2016-09-12T08:06:45-04:00' using my local timezone. It does convert it back to UTC when I query database through Django but I'd like to have 'clean' datetime in Postgres.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.