postgresql golang创建表错误,pq:“ $ 1”或附近的语法错误

Hi I don't understand what I'm doing wrong here.

_, err = db.Exec("CREATE TABLE $1", "books")
if err != nil {
    log.Fatal(err)
}

I'm failing to see what is syntactically wrong here.

I have also done:

_, err = db.Exec("CREATE TABLE books")
if err != nil {
    log.Fatal(err)
}

Gives the syntax error as well

Identifiers cannot be used as placeholders. Only values can be used there. (it's not a Go or its db drivers limitations, it's relational databases themselves that have such a "limitation").

In case of hardcoded identifiers - use the exact query

CREATE TABLE books

In case of dynamic placeholders - ensure you're using white lists of what values are allowed there.

UPD

Just this query

CREATE TABLE books

is invalid because it's missing any column definitions. It must be at least one column in a table.

is invalid because it's missing parentheses:

CREATE TABLE books()

in which you define columns.

TIL: you can have a table with zero columns in postgresql.

References: