1

I am not quite sure how to use the postgresqlConnect function in module Database.HaskellDB.HDBC.PostgreSQL to connect to a PostgreSQL database. The Haddock documentation page only states the type signature:

postgresqlConnect :: MonadIO m => [(String, String)] -> (Database -> m a) -> m a

What is a supposed to be?

How do I connect to a PostgreSQL server with postgresqlConnect?

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193

1 Answers1

2

This is what I guess: The first parameter is a list of options, if you have no specific options, just pass []. The next parameter is the code you actually want to run with the database. You get a Database argument and can do any monadic stuff with it. The postgresqlConnect function evaluates this monadic action, disconnects the databse and returns the result of it - m a. Most time you probably want to use IO a though.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • So whenever I want to query information from the database, I essentially need to connect and disconnect? That seems inefficient. – Daniel Trebbien Oct 08 '11 at 13:03
  • @Daniel The idea is that you execute the IO that needs database access inside one argument block, so you can send more than one queries. This style is needed because otherwise you could forget to close the connects. – fuz Oct 08 '11 at 13:06
  • 1
    The list of supported options is [here](http://www.postgresql.org/docs/8.1/static/libpq.html#LIBPQ-CONNECT). – hammar Oct 08 '11 at 13:46
  • @hammar I've never worked with postgresql before... thanks for the hint. – fuz Oct 08 '11 at 13:48