{"record":{"id":"6e14b92ca81f32c3","repo":"micro/go-micro","slug":"couldn-t-create-table-6e14b9","errorCode":null,"errorMessage":"Couldn't create table","messagePattern":"Couldn't create table","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/postgres/postgres.go","lineNumber":193,"sourceCode":"\t\tif strings.Contains(version, \"PostgreSQL\") {\n\t\t\t_, err = db.Exec(fmt.Sprintf(\"CREATE SCHEMA IF NOT EXISTS %s;\", database))\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\t}\n\n\t// Create a table for the namespace's prefix\n\t_, err = db.Exec(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s.%s\n\t(\n\t\tkey text NOT NULL,\n\t\tvalue bytea,\n\t\tmetadata JSONB,\n\t\texpiry timestamp with time zone,\n\t\tCONSTRAINT %s_pkey PRIMARY KEY (key)\n\t);`, database, table, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Couldn't create table\")\n\t}\n\n\t// Create Index\n\t_, err = db.Exec(fmt.Sprintf(`CREATE INDEX IF NOT EXISTS \"%s\" ON %s.%s USING btree (\"key\");`, \"key_index_\"+table, database, table))\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Create Metadata Index\n\t_, err = db.Exec(fmt.Sprintf(`CREATE INDEX IF NOT EXISTS \"%s\" ON %s.%s USING GIN (\"metadata\");`, \"metadata_index_\"+table, database, table))\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nfunc (s *sqlStore) configure() error {","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/postgres/postgres.go#L175-L211","documentation":"The legacy database/sql (lib/pq) postgres store creates the table during initDB (invoked from createDB and configure) and wraps failures with 'Couldn't create table'. This is the same DDL-initialization failure family as the pgx backend but on the older driver path. The wrapped cause contains the driver-level error.","triggerScenarios":"Configuring the store with a database URL where CREATE TABLE IF NOT EXISTS fails on first use: nonexistent database, insufficient privileges, invalid database/table name interpolation, or the database server being unreachable.","commonSituations":"Wrong POSTGRES_URL / connection string in environment; DB created without the app database; DBA restricted DDL; database name containing characters that break the interpolated identifier.","solutions":["Verify the connection string and that the target database exists (CREATE DATABASE if needed).","Grant CREATE privileges to the connecting user.","Check the wrapped error message for the exact driver/PostgreSQL error.","Pre-create the table manually if DDL is restricted.","Ensure database/table names are valid, quoted-safe identifiers."],"exampleFix":"// before\npostgres://user:pass@localhost/  (missing db name -> create table fails)\n// after\npostgres://user:pass@localhost/micro?sslmode=disable\n// plus: GRANT CREATE ON DATABASE micro TO user;","handlingStrategy":"validation","validationCode":"// before configuring the store\nconn, _ := sql.Open(\"postgres\", dsn)\nvar ok bool\nconn.QueryRow(\"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname=$1)\", dbName).Scan(&ok)\nvar priv bool\nconn.QueryRow(\"SELECT has_database_privilege($1, $2, 'CREATE')\", user, dbName).Scan(&priv)\n","typeGuard":null,"tryCatchPattern":"if err := store.Init(); err != nil {\n\tif strings.Contains(err.Error(), \"Couldn't create table\") {\n\t\t// check wrapped cause: run migrations or fix DSN, then retry\n\t\treturn fmt.Errorf(\"schema bootstrap failed, check grants/DSN: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Include the database name in the connection string and verify it exists before startup.","Use a bootstrap migration tool (golang-migrate) with a privileged role for DDL.","Grant CREATE to the application role or pre-create the table.","Quote or restrict database/table names to safe identifiers.","Smoke-test the DSN in CI against a real Postgres instance."],"tags":["postgres","sql","schema","database"],"backgroundTag":"sql-schema-migration-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}