{"record":{"id":"b54a4d88802b0a03","repo":"micro/go-micro","slug":"cannot-create-table","errorCode":null,"errorMessage":"cannot create table","messagePattern":"cannot create table","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store/postgres/pgx/pgx.go","lineNumber":96,"sourceCode":"\t\t}\n\t}\n\tdbObj := s.databases[database]\n\tif _, ok := dbObj.tables[table]; !ok {\n\t\terr := s.initTable(database, table)\n\t\tif err != nil {\n\t\t\treturn nil, Queries{}, err\n\t\t}\n\t}\n\n\treturn dbObj.conn, dbObj.tables[table], nil\n}\n\nfunc (s *sqlStore) initTable(database, table string) error {\n\tdb := s.databases[database].conn\n\n\t_, err := db.Exec(s.options.Context, fmt.Sprintf(createTable, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create table\")\n\t}\n\n\t_, err = db.Exec(s.options.Context, fmt.Sprintf(createMDIndex, table, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create metadata index\")\n\t}\n\n\t_, err = db.Exec(s.options.Context, fmt.Sprintf(createExpiryIndex, table, database, table))\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"cannot create expiry index\")\n\t}\n\n\ts.databases[database].tables[table] = NewQueries(database, table)\n\n\treturn nil\n}\n\nfunc (s *sqlStore) initDB(database string) error {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/postgres/pgx/pgx.go#L78-L114","documentation":"The pgx-backed SQL store runs a CREATE TABLE statement (via initTable) when a database/table is first accessed and wraps any execution error with 'cannot create table'. It means PostgreSQL rejected the DDL needed to materialize the store table. The wrapped cause carries the actual PostgreSQL error.","triggerScenarios":"First Read/Write/List against a database+table combination that has not been initialized, when the CREATE TABLE ... IF NOT EXISTS statement fails: syntax/permission errors, invalid database or table identifier, connection failure, or unsupported PostgreSQL version.","commonSituations":"DB user lacks CREATE privilege on the schema/database; connected to the wrong database name configured via store options; PostgreSQL version too old for the generated DDL; read-only replicas or restricted managed databases (e.g. limited cloud roles) blocking DDL.","solutions":["Grant CREATE privileges on the database/schema to the configured DB user: GRANT CREATE ON DATABASE <db> TO <user>.","Verify the configured database exists and the connection string points to it.","Check the wrapped error for the exact PostgreSQL message (e.g. permission denied, syntax error).","Pre-create the table manually if DDL is restricted in your environment.","Confirm PostgreSQL version compatibility with the store's DDL."],"exampleFix":"-- before\n-- user lacks privileges, CREATE TABLE fails\n-- after\nGRANT CREATE ON DATABASE micro TO micro_user;\n-- or pre-create:\n-- CREATE TABLE IF NOT EXISTS micro.keys (...);","handlingStrategy":"validation","validationCode":"// before using the store\nvar exists bool\nerr := adminConn.QueryRow(ctx, \"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname=$1)\", dbName).Scan(&exists)\n// and verify privileges:\n// SELECT has_database_privilege(current_user, $1, 'CREATE');","typeGuard":null,"tryCatchPattern":"if err := store.Init(); err != nil {\n\tvar pgErr *pgconn.PgError\n\tif errors.As(err, &pgErr) && pgErr.Code == \"42501\" { // insufficient privilege\n\t\treturn fmt.Errorf(\"db user lacks CREATE privilege: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Run schema bootstrap migrations with a privileged role before app startup.","Grant CREATE on the database/schema to the application user.","Validate the configured database name in the connection string at boot.","Keep PostgreSQL version within the store's supported range.","Test initialization against a local Postgres matching production settings."],"tags":["postgres","sql","schema","permissions"],"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"}