-
Notifications
You must be signed in to change notification settings - Fork 926
-
Howdy! Newbie here, trying to save remote IP address into Postgres with inet
data type.
I have this migration:
CREATE TABLE IF NOT EXISTS ips ( id bigserial PRIMARY KEY, ip INET );
And query:
-- name: CreateIp :one INSERT INTO ips ( ip ) VALUES ( $1 ) RETURNING *;
sqlc
generated this params struct:
type CreateIpParams struct { Ip net.IP `json:"ip"` }
Here's the code to save:
params := &db.CreateIpParams{} // Get IP from RemoteAddr ip, _, _ := net.SplitHostPort(r.RemoteAddr) if netIp := net.ParseIP(ip); netIp != nil { params.Ip = netIp } if _, err := db.NewPg(pg).CreateIp(context.Background(), *params); err != nil { render.Render(w, r, ErrRender(err)) return }
But it returns:
"error": "pq: invalid byte sequence for encoding \"UTF8\": 0x00"
So I removed the code that parse and save IP, it returns:
"error": "pq: invalid input syntax for type inet: \"\""
A SO user pointed out that it could be saving incorrect net.IP
type into inet
column. Also he suggested to override net.IP
to string
, but is there a way to use inet
type in Postgres?
sqlc (v1.5.0), go (v1.15), pg (13-alpine)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment