Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pq: syntax error at or near "$1" Postgres + go

Tags:

postgresql

go

What am i doing wrong here? Tried quoting new_name also , still shows error : pq: syntax error at or near "$1" Postgres + go

func ChangeDBname(new_name string) {

oldname := "intern"
quoted := pq.QuoteIdentifier(oldname)
_, e1 := db.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO $1",quoted) , new_name)
if e1 != nil {
    fmt.Println("Eroor in change name")
    log.Fatal(e1.Error())
} else {
    fmt.Println("Table name changed to", new_name)
}

}

like image 289
Captain bAri TV Avatar asked Oct 23 '25 21:10

Captain bAri TV


1 Answers

Table name is not a value. So PostgreSQL parser is not expecting a placeholder in this DDL.

Check this:

db.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s",pq.QuoteIdentifier(oldname), pq.QuoteIdentifier(new_name)))
like image 180
derkan Avatar answered Oct 26 '25 12:10

derkan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!