Why does my db/structure.sql file contain a CREATE SCHEMA statement after running db:structure:dump? Why does my db/structure.sql file contain a CREATE SCHEMA statement after running db:structure:dump? postgresql postgresql

Why does my db/structure.sql file contain a CREATE SCHEMA statement after running db:structure:dump?


My development environment config explicitly specified the "public" schema. I removed that specification, which allowed db:test:prepare to complete successfully.

# config/environments/development.rbRails.application.configure do  ...  config.active_record.dump_schemas = "public" # <<-- DELETED!end

You can configure which database schemas will be dumped when calling db:structure:dump by setting config.active_record.dump_schemas to one of the following options:

  • :schema_search_path: This looks for the schema names in the schema_search_path setting in config/database.yml.
  • "<some string>": It will dump the schema names in the string. Names here are comma separated values.
  • :all: No schema names are specified.
  • Or just don't set it at all, which is what I did.

If config.active_record.dump_schemas.blank? == true, ActiveRecord's Postgres adaptor will set the --schema=<schema name> flag on pg_dump, which in turn adds the line CREATE SCHEMA <schema name> to its output in db/structure.sql.

An interesting side effect is that now db:test:prepare inserts this instead:

CREATE SCHEMA _template;