Skip to content

Instantly share code, notes, and snippets.

@ahaxu
Last active December 6, 2022 02:43
Show Gist options
  • Save ahaxu/a847f0d6655d95ded85996f9a38be09f to your computer and use it in GitHub Desktop.
Save ahaxu/a847f0d6655d95ded85996f9a38be09f to your computer and use it in GitHub Desktop.
haskell_connect_db_with_options.hs
import Database.PostgreSQL.Simple
import System.Environment
import Database.PostgreSQL.Simple.Options
getDBConn :: IO Connection
getDBConn = do
-- get db info
hostName <- getEnv "DB_HOST"
dbName <- getEnv "DB_NAME"
userName <- getEnv "DB_USER"
dbPassword <- getEnv "DB_PASSWORD"
let
connOpts :: Options
connOpts = mempty
{ host = pure hostName
, user = pure userName
, dbname = pure dbName
, password = pure dbPassword
, port = pure 5432
}
connectPostgreSQL $ toConnectionString connOpts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment