Error Creating pgcrypto Extension

Поиск
Список
Период
Сортировка
Искать

Error Creating pgcrypto Extension

От:
Mark Hill <Mark.Hill@sas.com>
Дата:

We are trying to run this code in Postgres 18.6:
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;

and getting the error

postgres=# CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;

ERROR:  function "gen_random_uuid" already exists with same argument types

CONTEXT:  SQL statement "CREATE FUNCTION gen_random_uuid()

RETURNS uuid

AS '$libdir/pgcrypto', 'pg_random_uuid'

LANGUAGE C VOLATILE PARALLEL SAFE"

extension script file "pgcrypto--1.3.sql", near line 66

That’s because pgcrypto—1.3.sql has the following 4 lines starting at line 66:
CREATE FUNCTION gen_random_uuid()

RETURNS uuid

AS 'MODULE_PATHNAME', 'pg_random_uuid'

LANGUAGE C VOLATILE PARALLEL SAFE;


which try to create the gen_random_uuid() function but since it already exists it throws an error.

1)  Should pgcrytpo—1.3.sql be trying to create gen_random_uuid() if/when it already exists?


The pgcrypto.control file contains:
# pgcrypto extension

comment = 'cryptographic functions'

default_version = '1.4'

module_pathname = '$libdir/pgcrypto'

relocatable = true

trusted = true


2) given what’s in pgcrypto.control should there be a pgcrypto—1.4.sql file?


NOTE:  If I comment lines 66-69 in pgcrypto—1.3.sql the
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;
code completes successfully.

I’m hesitant to alter pgcrypto—1.3.sql.   Thoughts?

Thanks, Mark

 

Re: Error Creating pgcrypto Extension

От:
"David G. Johnston" <david.g.johnston@gmail.com>
Дата:
On Thu, Sep 17, 2026 at 3:25 PM Mark Hill <Mark.Hill@sas.com> wrote:

We are trying to run this code in Postgres 18.6:
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;

 

which try to create the gen_random_uuid() function but since it already exists it throws an error.

1)  Should pgcrytpo—1.3.sql be trying to create gen_random_uuid() if/when it already exists?


It's not expecting to be installed in the pg_catalog schema since it is an extension.  Extensions don't usually conditionally install their components.


2) given what’s in pgcrypto.control should there be a pgcrypto—1.4.sql file?


The pattern of a base full install and then incremental update scripts is supported.

David J.

Re: Error Creating pgcrypto Extension

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"David G. Johnston"  writes:
> On Thu, Sep 17, 2026 at 3:25 PM Mark Hill  wrote:
>> We are trying to run this code in Postgres 18.6:
>> *1)  Should pgcrytpo—1.3.sql be trying to create gen_random_uuid() if/when
>> it already exists?*

> It's not expecting to be installed in the pg_catalog schema since it is an
> extension.  Extensions don't usually conditionally install their components.

Yeah.  As of now, installing pgcrypto into pg_catalog is unsupported.

>> *2) given what’s in pgcrypto.control should there be a pgcrypto—1.4.sql
>> file?*

> The pattern of a base full install and then incremental update scripts is
> supported.

Installing 1.3 and then updating to a hypothetical 1.4 wouldn't work
anyway in this scenario, because installing 1.3 would fail.

We could perhaps decide that pgcrypto doesn't need to provide
gen_random_uuid anymore; maybe that backwards-compatibility hack
has served its time.  But I'm not sure if removing the function
would break anybody.  If we did go that way, we'd need to provide
a 1.3-to-1.4 update script that drops the function, and also a
pgcrypto--1.4 install script that rolls up all the prior updates
so that it never tries to create gen_random_uuid in the first place.

On the whole, seeing that installing pgcrypto into pg_catalog has
not worked since v13 and there's been hardly any complaints about
that, I'm not sure that we should risk breaking working setups to
allow it.

			regards, tom lane


Re: Error Creating pgcrypto Extension

От:
Daniel Gustafsson <daniel@yesql.se>
Дата:
> On 18 Sep 2026, at 02:05, Tom Lane  wrote:

> On the whole, seeing that installing pgcrypto into pg_catalog has
> not worked since v13 and there's been hardly any complaints about
> that, I'm not sure that we should risk breaking working setups to
> allow it.

I wouldn't be opposed to removing gen_random_uuid from pgcrypto, it's a hack
and documented as obsolete in all supported versions.  Supporting installing
pgcrypto in pg_catalog would not be a motivator though, trimming unnecessary
code from pgcrypto is however appealing

--
Daniel Gustafsson



RE: Error Creating pgcrypto Extension

От:
Phil Rau <Phil.Rau@sas.com>
Дата:
Good morning, everyone.

Thank you for the discussion so far.  I work with the original poster and I wanted to be sure that we're not trying to accomplish our goal in the wrong way. So, please allow me to describe what we are trying to do.

We are trying to make our product compliant with the Federal Information Processing Standard (FIPS).  So, in addition to converting from MD5 to SCRAM-SHA-256 password hashing and enabling TLS, we also needed to have the pgcrypto extension deployed to each database in our instance.  Our initial thought would be to create the extension at the instance level, and then have any databases created inside of that instance get the extension without any extra commands.  That's why we were tying this command:

CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;

However, Tom Lane indicated that this is not supported.  So, it seems that we are not using the right method.

The question now becomes:  Is there a way to set up an instance such that any new databases created underneath it automatically get a given extension (in this case pgcrypto)?  What is the best practice to do this?

For this discussion, we can assume that we are starting with a fresh instance, without any databases yet created inside of it.

Thank you!

--
Phil Rau

-----Original Message-----
From: Daniel Gustafsson  
Sent: Friday, September 18, 2026 5:44 AM
To: Tom Lane 
Cc: David G. Johnston ; Mark Hill ; pgsql-general ; Phil Rau ; Michael King ; Ashley Stephens 
Subject: Re: Error Creating pgcrypto Extension

> On 18 Sep 2026, at 02:05, Tom Lane  wrote:

> On the whole, seeing that installing pgcrypto into pg_catalog has not 
> worked since v13 and there's been hardly any complaints about that, 
> I'm not sure that we should risk breaking working setups to allow it.

I wouldn't be opposed to removing gen_random_uuid from pgcrypto, it's a hack and documented as obsolete in all supported versions.  Supporting installing pgcrypto in pg_catalog would not be a motivator though, trimming unnecessary code from pgcrypto is however appealing

--
Daniel Gustafsson



Re: Error Creating pgcrypto Extension

От:
Adrian Klaver <adrian.klaver@aklaver.com>
Дата:
On 9/18/26 7:33 AM, Phil Rau wrote:
> Good morning, everyone.
> 
> Thank you for the discussion so far.  I work with the original poster and I wanted to be sure that we're not trying to accomplish our goal in the wrong way. So, please allow me to describe what we are trying to do.
> 
> We are trying to make our product compliant with the Federal Information Processing Standard (FIPS).  So, in addition to converting from MD5 to SCRAM-SHA-256 password hashing and enabling TLS, we also needed to have the pgcrypto extension deployed to each database in our instance.  Our initial thought would be to create the extension at the instance level, and then have any databases created inside of that instance get the extension without any extra commands.  That's why we were tying this command:
> 
> CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;
> 
> However, Tom Lane indicated that this is not supported.  So, it seems that we are not using the right method.
> 
> The question now becomes:  Is there a way to set up an instance such that any new d

https://www.postgresql.org/docs/18/manage-ag-templatedbs.html

"CREATE DATABASE actually works by copying an existing database. By 
default, it copies the standard system database named template1. Thus 
that database is the “template” from which new databases are made. If 
you add objects to template1, these objects will be copied into 
subsequently created user databases. ... "

atabases created underneath it automatically get a given extension (in 
this case pgcrypto)?  What is the best practice to do this?
> 
> For this discussion, we can assume that we are starting with a fresh instance, without any databases yet created inside of it.
> 
> Thank you!
> 
> --
> Phil Rau
> 
> -----Original Message-----
> From: Daniel Gustafsson 
> Sent: Friday, September 18, 2026 5:44 AM
> To: Tom Lane 
> Cc: David G. Johnston ; Mark Hill ; pgsql-general ; Phil Rau ; Michael King ; Ashley Stephens 
> Subject: Re: Error Creating pgcrypto Extension
> 
>> On 18 Sep 2026, at 02:05, Tom Lane  wrote:
> 
>> On the whole, seeing that installing pgcrypto into pg_catalog has not
>> worked since v13 and there's been hardly any complaints about that,
>> I'm not sure that we should risk breaking working setups to allow it.
> 
> I wouldn't be opposed to removing gen_random_uuid from pgcrypto, it's a hack and documented as obsolete in all supported versions.  Supporting installing pgcrypto in pg_catalog would not be a motivator though, trimming unnecessary code from pgcrypto is however appealing
> 
> --
> Daniel Gustafsson
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com


Re: Error Creating pgcrypto Extension

От:
Karsten Hilbert <Karsten.Hilbert@gmx.net>
Дата:
Am Fri, Sep 18, 2026 at 02:33:33PM +0000 schrieb Phil Rau:

> The question now becomes:  Is there a way to set up an instance such that any new databases created underneath it automatically get a given extension (in this case pgcrypto)?  What is the best practice to do this?

Isn't the canonical way for that to install it into a
template database from which new databases get created
eventually ?

Karsten
-- 
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B


Re: Error Creating pgcrypto Extension

От:
Adrian Klaver <adrian.klaver@aklaver.com>
Дата:
On 9/18/26 7:41 AM, Adrian Klaver wrote:
> On 9/18/26 7:33 AM, Phil Rau wrote:
>> Good morning, everyone.
>>
>> Thank you for the discussion so far.  I work with the original poster 
>> and I wanted to be sure that we're not trying to accomplish our goal 
>> in the wrong way. So, please allow me to describe what we are trying 
>> to do.
>>
>> We are trying to make our product compliant with the Federal 
>> Information Processing Standard (FIPS).  So, in addition to converting 
>> from MD5 to SCRAM-SHA-256 password hashing and enabling TLS, we also 
>> needed to have the pgcrypto extension deployed to each database in our 
>> instance.  Our initial thought would be to create the extension at the 
>> instance level, and then have any databases created inside of that 
>> instance get the extension without any extra commands.  That's why we 
>> were tying this command:
>>
>> CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA pg_catalog CASCADE;
>>
>> However, Tom Lane indicated that this is not supported.  So, it seems 
>> that we are not using the right method.
>>
>> The question now becomes:  Is there a way to set up an instance such 
>> that any new d
> 
> https://www.postgresql.org/docs/18/manage-ag-templatedbs.html
> 
> "CREATE DATABASE actually works by copying an existing database. By 
> default, it copies the standard system database named template1. Thus 
> that database is the “template” from which new databases are made. If 
> you add objects to template1, these objects will be copied into 
> subsequently created user databases. ... "

In previous post I should have added a caveat, that from here:

https://www.postgresql.org/docs/18/sql-createdatabase.html

"template

     The name of the template from which to create the new database, or 
DEFAULT to use the default template (template1).
"

So it is possible for someone to create a database using a template 
database other then template1.

> 
> atabases created underneath it automatically get a given extension (in 
> this case pgcrypto)?  What is the best practice to do this?
>>
>> For this discussion, we can assume that we are starting with a fresh 
>> instance, without any databases yet created inside of it.
>>
>> Thank you!
>>
>> -- 
>> Phil Rau
>>
Adrian Klaver
adrian.klaver@aklaver.com


Re: Error Creating pgcrypto Extension

От:
Tim Gerber <tggerber@gmail.com>
Дата:
There's a few ways to accomplish this.  

As others have mentioned, pg_catalog isn't the best way to go about this.   From a schema perspective, you can install it in public or even better, create a dedicated schema for all extensions (i.e. create schema extensions;) and then place all of your extensions in there.  My preference is to go with the dedicated extensions schema and then make sure the extensions schema is part of the search_path.   

To have pgcrypto (and any author extensions) get installed automatically when you run a create database, you can create the extensions schema in the template 1 database and install the needed extensions there.  Alternatively, if you don't want to mess with the template 1 database, you can create a new template database and then set that to be the default by issuing an ALTER DATABASE ... IS_TEMPLATE TRUE;   (see https://www.postgresql.org/docs/current/sql-alterdatabase.html).  

Personally, if I was doing this, I'd automate this with ansible (or your favorite automation tool) and have the automation code create the database, create the extensions schema,  create the extensions in the new schema, and finally set the search_path,.

Best Regards,
Tim

On Fri, Sep 18, 2026 at 9:44 AM Karsten Hilbert <Karsten.Hilbert@gmx.net> wrote:
Am Fri, Sep 18, 2026 at 02:33:33PM +0000 schrieb Phil Rau:

> The question now becomes:  Is there a way to set up an instance such that any new databases created underneath it automatically get a given extension (in this case pgcrypto)?  What is the best practice to do this?

Isn't the canonical way for that to install it into a
template database from which new databases get created
eventually ?

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B


FAQ