micro/go-micro · error
Couldn't create table
Error message
Couldn't create table
What it means
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.
Source
Thrown at store/postgres/postgres.go:193
if strings.Contains(version, "PostgreSQL") {
_, err = db.Exec(fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s;", database))
if err != nil {
return err
}
}
}
// Create a table for the namespace's prefix
_, err = db.Exec(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s.%s
(
key text NOT NULL,
value bytea,
metadata JSONB,
expiry timestamp with time zone,
CONSTRAINT %s_pkey PRIMARY KEY (key)
);`, database, table, table))
if err != nil {
return errors.Wrap(err, "Couldn't create table")
}
// Create Index
_, err = db.Exec(fmt.Sprintf(`CREATE INDEX IF NOT EXISTS "%s" ON %s.%s USING btree ("key");`, "key_index_"+table, database, table))
if err != nil {
return err
}
// Create Metadata Index
_, err = db.Exec(fmt.Sprintf(`CREATE INDEX IF NOT EXISTS "%s" ON %s.%s USING GIN ("metadata");`, "metadata_index_"+table, database, table))
if err != nil {
return err
}
return nil
}
func (s *sqlStore) configure() error {View on GitHub (pinned to 24529f1404)
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.
Example fix
// before postgres://user:pass@localhost/ (missing db name -> create table fails) // after postgres://user:pass@localhost/micro?sslmode=disable // plus: GRANT CREATE ON DATABASE micro TO user;
Defensive patterns
Strategy: validation
Validate before calling
// before configuring the store
conn, _ := sql.Open("postgres", dsn)
var ok bool
conn.QueryRow("SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname=$1)", dbName).Scan(&ok)
var priv bool
conn.QueryRow("SELECT has_database_privilege($1, $2, 'CREATE')", user, dbName).Scan(&priv)
Try / catch
if err := store.Init(); err != nil {
if strings.Contains(err.Error(), "Couldn't create table") {
// check wrapped cause: run migrations or fix DSN, then retry
return fmt.Errorf("schema bootstrap failed, check grants/DSN: %w", err)
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- model/postgres: create table: %w
- model/postgres: create: %w
- model/postgres: update: %w
- cannot create table
- Database connection not initialized
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/6e14b92ca81f32c3.
Report an issue: GitHub.