You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Howdy! Newbie here, trying to save remote IP address into Postgres with inet data type.
I have this migration:
CREATETABLEIF NOT EXISTS ips (
id bigserialPRIMARY KEY,
ip INET
);
And query:
-- name: CreateIp :oneINSERT INTO ips (
ip
) VALUES (
$1
) RETURNING *;
sqlc generated this params struct:
typeCreateIpParamsstruct {
Ip net.IP`json:"ip"`
}
Here's the code to save:
params:=&db.CreateIpParams{}
// Get IP from RemoteAddrip, _, _:=net.SplitHostPort(r.RemoteAddr)
ifnetIp:=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?
This discussion was converted from issue #726 on October 19, 2020 06:43.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Howdy! Newbie here, trying to save remote IP address into Postgres with
inet
data type.I have this migration:
And query:
sqlc
generated this params struct:Here's the code to save:
But it returns:
So I removed the code that parse and save IP, it returns:
A SO user pointed out that it could be saving incorrect
net.IP
type intoinet
column. Also he suggested to overridenet.IP
tostring
, but is there a way to useinet
type in Postgres?sqlc (v1.5.0), go (v1.15), pg (13-alpine)
On SO: https://stackoverflow.com/questions/64226493/how-to-save-remote-ip-address-in-postgres-with-go?noredirect=1#comment113575066_64226493
Beta Was this translation helpful? Give feedback.
All reactions