-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
37 lines (30 loc) · 730 Bytes
/
main.go
File metadata and controls
37 lines (30 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/jackc/pgx/v5"
)
func main() {
dbURL := os.Getenv("DATABASE_URL")
if dbURL == "" {
fmt.Fprintln(os.Stderr, "DATABASE_URL environment variable is required")
os.Exit(1)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
conn, err := pgx.Connect(ctx, dbURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close(ctx)
var count int
err = conn.QueryRow(ctx, "SELECT COUNT(*) FROM public.user").Scan(&count)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to query users count: %v\n", err)
os.Exit(1)
}
fmt.Println("users:", count*3)
}