go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/dbutil/db_exists.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbutil 9 10 import ( 11 "context" 12 13 "go.charczuk.com/sdk/db" 14 ) 15 16 // DatabaseExists returns if a database exists or not. 17 func DatabaseExists(ctx context.Context, name string, opts ...db.Option) (exists bool, err error) { 18 var conn *db.Connection 19 defer func() { 20 err = PoolCloseFinalizer(conn, err) 21 }() 22 23 conn, err = OpenManagementConnection(opts...) 24 if err != nil { 25 return 26 } 27 28 exists, err = conn.QueryContext(ctx, "SELECT 1 FROM pg_database WHERE datname = $1", name).Any() 29 return 30 }