Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgx support? #16

Open
tamis-laan opened this issue Aug 10, 2023 · 0 comments
Open

pgx support? #16

tamis-laan opened this issue Aug 10, 2023 · 0 comments

Comments

@tamis-laan
Copy link

I'm trying to use pgx to insert and retrieve a gps point:

	// Establish a connection to the database
	conn, err := pgx.Connect(context.Background(), <CONNECTION_STRING>)
	if err != nil {
		log.Fatal(err)
	}

	// Ensure we have PostGIS on the database
	_, err = conn.Exec(context.Background(), "CREATE EXTENSION IF NOT EXISTS postgis")
	if err != nil {
		log.Fatal(err)
	}

	// Create the table with a PostGIS column
	_, err = conn.Exec(context.Background(), `
		CREATE TABLE IF NOT EXISTS gps_points (
			id SERIAL PRIMARY KEY,
			geom geometry(Point, 4326)
		);
	`)
	if err != nil {
		log.Fatal(err)
	}

	// Construct the PointS struct
	point := pq_types.PostGISPoint{Lon: 37.6088900, Lat: 55.8219130}

	// Insert the GPS point into the table
	_, err = conn.Exec(context.Background(), "INSERT INTO gps_points (geom) VALUES (ST_GeomFromText($1));", point)
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve the GPS point from the database
	var retrievedPoint pq_types.PostGISPoint
	err = conn.QueryRow(context.Background(), "SELECT geom FROM gps_points WHERE id = $1;", 1).Scan(&retrievedPoint)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Retrieved GPS point: %+v\n", retrievedPoint)

But I get back:

can't scan into dest[0]: PostGISPoint.Scan: expected []byte, got string (0101000020E6100000B98D06F0162055C0AF25E4839E8D4340)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant