So I'm pretty new to Post gres and am trying to set the value of a time field to be the current time.
UPDATE table_name
SET updated_at = now(),
WHERE id = :id
I'm executing this with golang and the above syntax errors.
How do I enter the current time into the updated_at time field?
As point out by someone you have an extra ,
that is causing the issue. You can form the string correctly (query
in the below example)and pass it to the db API.
package main
import (
"fmt"
"time"
)
func main() {
name := "table_name"
id := "table_id"
query := fmt.Sprintf("UPDATE %s SET update_at = %q WHERE id = %s", name, time.Now(), id)
}
As you mentioned
You want to set the value of a time field as current time
You can try this, Now()
provides current DateTime. After casting as Time
we can get currenttime
UPDATE table_name
SET updated_at = now()::TIME
WHERE id = _id
Alternately you can try
UPDATE table_name
SET updated_at = current_time --gives you current time with time zone
WHERE id = _id
please refer https://www.postgresql.org/docs/9.1/static/functions-datetime.html for more details.
Hope it works for you.