How to initialize a PostGIS database in PostgreSQL single-user mode? I'm using PostgreSQL 9.2.15 with PostGIS 2.0.7 through the standard packages available on CentOS 7 with EPEL.

I have a server script to automatically setup a PostGIS database, by first executing initdb, then using postgres --single to create a new database with PostGIS extension. This works roughly like this:

initdb -D ...

postgres --single -D ... postgres &lt;&lt;'EOF'
    CREATE DATABASE mydb;
EOF

postgres --single -D ... mydb &lt;&lt;'EOF'
    CREATE EXTENSION postgis;
EOF


However, the last command produces the following strange error message:

ERROR:  type reclassarg[] does not exist
STATEMENT:  CREATE EXTENSION postgis;


If I instead run the database server and execute CREATE EXTENSION postgis via sql, everything works fine:

postgres -D ... &amp;

# Wait until service it up and running

psql mydb &lt;&lt;'EOF'
    CREATE EXTENSION postgis;
EOF


But that is not suitable for a database initialization script.

So how can I initialize a PostGIS database in PostgreSQL single-user mode?

Update: There is  a thread in the pgsql-bugs mailinglist on that topic, but that doesn't answer my question. It just explains that it is not planned to improve the single-user mode of PostgreSQL. So there will always be missing PgSQL language features to execute the current PostGIS extension creation script. However, most other PostgreSQL extensions can be installed in single-user mode just fine. So maybe there is a way to reformulate the PostGIS creation script so it achieves the same thing without requiring those language features, i.e. to make it "more portable".

(This question originally appeared on StackExchange / GIS: https://gis.stackexchange.com/q/214449/82644)
Someone previously ask a very similar question on the lists. Anyway, as told in the thread, reclassarg is a composite type.


  RhodiumToad: Guest78493: in single-user, doing CREATE TYPE ... AS ...; 
  doesn't create the array type
  
  RhodiumToad: Guest78493: it creates only the non-array one, but I have no idea yet why
  
  RhodiumToad: Guest78493: so for now, any extension with a CREATE TYPE AS won't load correctly in single-user
  
  RhodiumToad: Guest78493: arguably if it doesn't work it should error rather than quietly getting it wrong, so I think you're justified in calling it a postgres bug


Further, from Tom Lane himself!


  
    [ Can't create postgis extension in single-user mode ]
  
  
  Why in the world would you think that's a good thing to do?
  
  Single-user mode is a barely-documented disaster recovery aid.
  It's not meant for routine activity.  There are a whole lot
  of behaviors you want that are turned off in single-user mode.
  
  The specific reason why this doesn't work is this bit in
  heap_create_with_catalog:

/*
 * Decide whether to create an array type over the relation's rowtype. We
 * do not create any array types for system catalogs (ie, those made
 * during initdb). We do not create them where the use of a relation as
 * such is an implementation detail: toast tables, sequences and indexes.
 */
if (IsUnderPostmaster &amp;&amp; (relkind == RELKIND_RELATION ||
                          relkind == RELKIND_VIEW ||
                          relkind == RELKIND_MATVIEW ||
                          relkind == RELKIND_FOREIGN_TABLE ||
                          relkind == RELKIND_COMPOSITE_TYPE))
    new_array_oid = AssignTypeArrayOid();

  
  We could possibly develop some other mechanism for detecting whether
  we're within the initdb sequence, but I can't get very excited about
  treating this as a bug.  Single-user mode hasn't been considered a
  standard user environment since maybe the early 90s.


If it was you, please update us in the future and self-answer your questions when you get word from the horse's mouth.

UPDATE.


  So maybe there is a way to reformulate the PostGIS creation script so it achieves the same thing without requiring those language features, i.e. to make it "more portable".


No, there is not. ST_Reclass requires it. The method you're using is not supported. Initializing a database is currently viewed as a different thing than CREATE EXTENSION on one. I don't think PostgreSQL intends -single to be used in that way, and certainly PostGIS doesn't intent to rewrite working code so it functions in PostgreSQL's unsupported modes, with outdated single-user contexts.