Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to save remote IP address in Postgres with inet? #726

Unanswered
fookit asked this question in Q&A
Discussion options

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)

On SO: https://stackoverflow.com/questions/64226493/how-to-save-remote-ip-address-in-postgres-with-go?noredirect=1#comment113575066_64226493

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #726 on October 19, 2020 06:43.

AltStyle によって変換されたページ (->オリジナル) /