Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o0sa6-0001f8-Vj for pgsql-general@arkaria.postgresql.org; Mon, 13 Jun 2022 22:30:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o0sa5-0003yp-Mm for pgsql-general@arkaria.postgresql.org; Mon, 13 Jun 2022 22:30:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o0sa4-0003yg-2u for pgsql-general@lists.postgresql.org; Mon, 13 Jun 2022 22:30:45 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o0sZz-0006A9-IL for pgsql-general@lists.postgresql.org; Mon, 13 Jun 2022 22:30:43 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 6B4685C018A; Mon, 13 Jun 2022 18:30:36 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 13 Jun 2022 18:30:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1655159436; x= 1655245836; bh=jcEUmt4XPHVdiT3qOz8iyP6XMGTYbPWDFvZeW8HfjWc=; b=C 8ahlDUbK6Qtz904TVANWUbfjAwo8jpaCvCAzzGr2pItkHNWzs2ha5cBGxxHodeDy f+qVc4DnR8CFPImRIzRB+GoS3qyzohVDF3dhLOY3iAG69Kg15UrlSlOL3thsYUUC LsnccpHGyfG6KInH3Z3DnrlRWRSsI5gtIWaW4Nvwp3hxiJ3j/71yFjiFZgpoIW2P ftrcpwpwjGc/77/5+ZhRxsmU/C+/KbY5GTk4EnsCQ6ADKHMKE3Y6Gdo8pNv30PAG 1xFVzNA/AzyQqt4gJb6V4F0e7BuAAX46FX4eEJiRczbxOVCqTzf1FfdFqJSEeYCv 9XWxi35S6RV+ppaQacnvQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1655159436; x=1655245836; bh=j cEUmt4XPHVdiT3qOz8iyP6XMGTYbPWDFvZeW8HfjWc=; b=oIIDnjNBux5O2too5 yIfusPZclYmeKGqII34PNsPkg/I3AfbuF1KglzJXcwnHHLVTqnGPvY9Dhik1V6Jp /azWQ9fQF/7PZaScwB1qVPMYWg1wIdl/2JpGDKYArf3P4OLnBAYLPvNO6FbHlTTY UHscVjO+9dEd3UEj4227syRi0OzKa0/k6lDwqMTj8/Q5e07KekFdq/l4g2xXcNCt ZxoLAxW9vXEGlLfzbyVa8nN3Hgl0bIcCkej8fgfRgnlnxIGlSe00zzsn0F/M8wui WygYfPNKQnj4UUKhuWoWmQF2VLPPdoDGFFKsyEhp25/aCOfWWOtez91O0QLpUcP0 EZSCA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedruddukedguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefheenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpedvkefhhefhleekjeeuueelhedvgeejfeehlefftefh tdefffefgeetfffgheefffenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 Jun 2022 18:30:35 -0400 (EDT) Subject: Re: Tools to convert timestamp data to another time zone in PostgreSQL To: Joel Rabinovitch , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: <9e4246c7-5fc4-65c7-c5f2-9d5122689d34@aklaver.com> Date: Mon, 13 Jun 2022 15:30:34 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/13/22 2:52 PM, Joel Rabinovitch wrote: > Hi, > > We have recently modified our application to work with PostgreSQL > databases and schemas. We also support Oracle and SQL Server Databases. > > Along with adding support for PostgreSQL, we have upgraded our > infrastructure such that all environments are configured to use the UTC > time zone. Previously, the environments were configured to use the time > zone where the database server and application server were installed. > > As a result, we have hit an issue where we need to convert data in > timestamp columns in existing records to reflect that the time is in > UTC. The timezone is not specified in our timestamp columns (i.e. they > are defined as timezone without time zone). We need to do this for > interoperability between the database engines we support. > > After a bit of searching, we found we can write an SQL similar to the > one below to do the conversion: > > update client > >    set create_stamp = (create_stamp at time zone 'America/New_York' at > time zone 'UTC') > > where client_code = 'HOANA'; > > This does work correctly. However, we have some limitations in terms > using SQL statements like this. > > - We would need to identify the timestamp columns that would be affected > across many tables and multiple schemas. select table_schema, table_name, column_name from information_schema.columns where data_type = 'timestamp without time zone'; > > - We also store date-only information in timestamp without time zone > columns. This was done as a result of migrating our application from > Oracle where the DATE data type was used at the time (Oracle now > supports timestamp columns). A date stored in a timestamp field is going to be a timestamp at midnight: timestamp_test Table "public.timestamp_test" Column | Type | Collation | Nullable | Default --------+-----------------------------+-----------+----------+--------- ts | timestamp without time zone | | | tsz | timestamp with time zone | insert into timestamp_test values (current_date, current_date); ts | tsz -------------------------+---------------------------- 2022-06-13 00:00:00 | 2022-06-13 00:00:00-07 > > I was wondering if you are aware of any open source and/or commercial > tools that could allow us to easily identify the affected columns, > exclude columns if necessary, and apply the necessary conversion. If > not, we would have to write a utility that does this for us, which could > be a lengthy process. > > Thanks, > > Joel > -- Adrian Klaver adrian.klaver@aklaver.com