Skip to content

pgx support? #16

@tamis-laan

Description

@tamis-laan

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions