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 1k1Iup-0007wl-48 for pgsql-sql@arkaria.postgresql.org; Fri, 31 Jul 2020 00:28:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k1Iuo-0001vU-1W for pgsql-sql@arkaria.postgresql.org; Fri, 31 Jul 2020 00:28:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k1Iun-0001vN-Oh for pgsql-sql@lists.postgresql.org; Fri, 31 Jul 2020 00:28:49 +0000 Received: from mail-pl1-x643.google.com ([2607:f8b0:4864:20::643]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k1Iul-0004eL-E5 for pgsql-sql@lists.postgresql.org; Fri, 31 Jul 2020 00:28:48 +0000 Received: by mail-pl1-x643.google.com with SMTP id w17so15762181ply.11 for ; Thu, 30 Jul 2020 17:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=z2nBnS7/qirzstMqa74Ts6/wGjxX3USe912EyTSQyhc=; b=vRlptepWSSeUXW/VBF1yUcLfvNwRHgY4p+fMK46w787QPwEsuWe5ubNYKHdud2+OoR Dvu0qsAvGSm6hZmHnKvjH6W2jKImRNDBZG1MyadftGlnp0RXRiRmzW8qEojnoj0wuoWi t2TC6+ZBP/oAbmgOt9ADmvx2812B37NbD8hwUYu5mvIRJuWpAcvhysDM8+rSsfDtUWDa vyxD6ZR2Ft0uZ17fCNCPQ7nv2uUjpZ9LrmrOqeYFbQ2PSLT9ecEeAnE7e1kfEB1l7TKa BLi3ZlJpaDssx1CDDq/MC6gnlaBRDV57qoEv2YpKKF0n2AFqQ6EO60SNFFn30Yglall+ WobA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=z2nBnS7/qirzstMqa74Ts6/wGjxX3USe912EyTSQyhc=; b=rnGVf+nsBKZNmfcSlT//d6F1uhX22YUq7OI2vUl1R++kDydQ0QoyyinR3aUNSeFQ5z 0+LZ3e0nC9iAm4SPdEgmRGhjST59zhTemB5mzb3G82luV5n+E5zVWsF/rHUiUaQ5vV9o Cujdk2zMnv25WRCn/z59rlHDoQ1FbHyKP8S5mZggtXXIZXVY6In8LfEK/QqZG5KAmUJa oFPiCL6VPOBBW2YqZ3oGaW1Kdfry8X30YcHLEhuWKLJN9VB/VlyV0kJGoTyR3XZg2+U2 wHs0G4g0+6xALL3YyQRmBfXuuSIzutjK+fWNd0PLyCyO1T8j4P70pM5hrAjb44b9q4pQ 6j0Q== X-Gm-Message-State: AOAM533OawRbxRiO7149NKzr25QcTbHSiEXedxURseFXSJhGMydVc8gP wxaRxXvZvf5YVlarEn3TbwjrquF9aw0= X-Google-Smtp-Source: ABdhPJzWx90D18zSkpJhUR9nxtawpm/fRRYSpC1eeg5i93eVnuWREpLZ1NVs0+SkyAWVbF7Q17mRnw== X-Received: by 2002:a65:558d:: with SMTP id j13mr1376812pgs.48.1596155326124; Thu, 30 Jul 2020 17:28:46 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:acf7:ec2b:231f:25e4? ([2601:681:5500:dde0:acf7:ec2b:231f:25e4]) by smtp.gmail.com with ESMTPSA id x7sm7638579pfc.209.2020.07.30.17.28.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Jul 2020 17:28:45 -0700 (PDT) Subject: Re: how to use function from different database To: pgsql-sql@lists.postgresql.org References: From: Rob Sargent Message-ID: <582718df-7d99-5601-675c-bf2125d2e689@gmail.com> Date: Thu, 30 Jul 2020 18:28:44 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-CA Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/30/20 6:03 PM, Bhupendra Babu wrote: > Hi, > > I created a generic function (DATEFIFF) login to postgres database using > psql. ANd I wanted to use across the entire instance , from all other > databases. > > postgres=> CREATE FUNCTION DateDiff (units VARCHAR(30), start_t > TIMESTAMP, end_t TIMESTAMP) > postgres->      RETURNS INT AS $$ > postgres$>    DECLARE > postgres$>      diff_interval INTERVAL; > ... > ... > > grant execute on function DATEDIFF to public; > grant execute on function DATEDIFF(varchar,timestamp,timestamp) to public; > > I tried both the grant one at a time, none of them works. > ------------------------------------------ > Connected to another database > \c application1 > > application1=> SELECT * from DATEDIFF('second', '2011-12-30 > 08:54:55'::timestamp, '2011-12-30 08:56:10'::timestamp); > ERROR:  function datediff(unknown, timestamp without time zone, > timestamp without time zone) does not exist > LINE 1: SELECT * from DATEDIFF('second', '2011-12-30 08:54:55'::time... >                       ^ > HINT:  No function matches the given name and argument types. You might > need to add explicit type casts. > application1=> > ------------------------------------------ > > How can I use this function across my entire postgresql instance from > any databases ? > > -- > Thanks. > Bhupendra B Babu You can create public.datadiff in the template1 database. Thereafter all created databases in that cluster will get the function. However, for existing databases, you'll need to apply the function by hand. I recommend using the public schema explicitly. It should be fairly straight forward to script this. superuser privilege is of course required.