In a forum post from July 2009 on the SQL-Ledger User Forum, a user named Matt (username “red69p51”) reported an issue while creating a new database using SQL-Ledger version 2.8.24 with PostgreSQL 8.3.7 on Fedora 11. He encountered the following error message:
Error!
CREATE SEQUENCE id start 10000
ERROR: relation "id" already exists
Matt traced the error to specific lines in the Pg-tables.sql file, which attempt to create a sequence named “id”:
--
CREATE SEQUENCE id start 10000;
SELECT nextval ('id');
--
He questioned whether his upgrade missed something or if this was a known issue with that version of PostgreSQL.
Another forum member, “dws,” responded by explaining that the error serves as a security measure to prevent accidental overwriting of existing datasets.
Matt replied, indicating that he was unable to create any new SQL-Ledger databases due to this error, despite not overwriting any existing datasets. He mentioned having other SQL-Ledger databases on the same system with different names.
Upon further investigation, Matt discovered that the PostgreSQL template1 database contained both “basex” and “id” sequences and asked if this was normal.
“dws” clarified that this was not standard behavior. The template1 database in PostgreSQL is used as a template for creating new databases; any objects within it are copied into new databases upon creation. Since “id” already existed in template1, any new database would inherit this sequence, causing the CREATE SEQUENCE id command to fail due to the sequence’s prior existence.
Solution:
The root cause of the issue was that template1 had been inadvertently modified to include objects (like the “id” sequence) that should not be present. The recommended solution was to clean up the template1 database by removing the unintended objects. This would prevent the conflict when creating new databases, allowing SQL-Ledger’s setup scripts to execute without errors.
By ensuring that template1 only contains the default objects provided by PostgreSQL, users can avoid similar conflicts when creating new databases in SQL-Ledger.