Skip to content

Using SELECT EXISTS #1427

Answered by jackc
lukewhrit asked this question in Q&A
Dec 13, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Works for me:

package main

import (
	"context"
	"log"
	"os"

	"github.com/jackc/pgx/v5"
)

func main() {
	ctx := context.Background()

	conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close(ctx)

	var b bool
	err = conn.QueryRow(ctx, "select exists(select 1 from pg_type where typname = 'int4')").Scan(&b)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(b)
}

Output:

2022/12/15 20:30:35 true

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@lukewhrit
Comment options

Answer selected by lukewhrit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants