hasql: Fast PostgreSQL driver with a flexible mapping API

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

Root of the "hasql" ecosystem. This library provides connection management, execution of queries and mapping of parameters and results. Extended functionality such as pooling, transactions and compile-time checking of SQL is provided by extension libraries. For more details and tutorials see the readme.

The API comes free from all kinds of exceptions. All error-reporting is explicit and is presented using the Either type.

"hasql" requires you to have the "libpq" C-library of at least version 14 installed to compile. "libpq" comes distributed with PostgreSQL, so typically all you need is just to install the latest PostgreSQL distro.

Despite the mentioned requirements for "libpq" "hasql" is thoroughly tested to be compatible with a wide range of PostgreSQL servers starting from version 9.


[Skip to Readme]

Properties

Versions 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.3.0, 0.4.0, 0.4.1, 0.5.0, 0.6.0, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.7.3.1, 0.7.3.2, 0.7.4, 0.14.0.2, 0.14.0.3, 0.15.0.2, 0.15.1, 0.15.1.1, 0.19.2, 0.19.3, 0.19.3.1, 0.19.3.2, 0.19.3.3, 0.19.4, 0.19.5, 0.19.6, 0.19.7, 0.19.8, 0.19.9, 0.19.10, 0.19.11, 0.19.12, 0.19.13, 0.19.14, 0.19.15, 0.19.15.1, 0.19.15.2, 0.19.16, 0.19.17, 0.19.17.1, 0.19.18, 0.19.18.1, 0.19.18.2, 0.20, 0.20.0.1, 0.20.1, 1, 1.1, 1.1.1, 1.2, 1.3, 1.3.0.1, 1.3.0.2, 1.3.0.3, 1.3.0.5, 1.3.0.6, 1.4, 1.4.0.1, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.4.4.1, 1.4.4.2, 1.4.4.3, 1.4.5, 1.4.5.1, 1.4.5.2, 1.4.5.3, 1.5, 1.5.0.1, 1.5.0.2, 1.5.0.3, 1.5.0.4, 1.5.0.5, 1.5.1, 1.6, 1.6.0.1, 1.6.1, 1.6.1.1, 1.6.1.2, 1.6.1.3, 1.6.1.4, 1.6.2, 1.6.3, 1.6.3.1, 1.6.3.2, 1.6.3.3, 1.6.3.4, 1.6.4, 1.6.4.1, 1.6.4.2, 1.6.4.3, 1.6.4.4, 1.7, 1.7.0.1, 1.7.0.2, 1.8, 1.8.0.1, 1.8.0.2, 1.8.1, 1.8.1.1, 1.8.1.2, 1.8.1.3, 1.8.1.4, 1.9, 1.9.1, 1.9.1.1, 1.9.1.2, 1.9.2, 1.9.3, 1.9.3.1, 1.9.3.2, 1.10, 1.10.1, 1.10.2, 1.10.2.1, 1.10.2.2, 1.10.2.3, 1.10.2.4, 1.10.3, 1.10.3.1, 1.10.3.2, 1.10.3.3, 1.10.3.4, 1.10.3.5, 1.10.3.6, 1.10.3.7, 2.0.0.0
Change log CHANGELOG.md
Dependencies aeson (>=2 && <3), attoparsec (>=0.10 && <0.15), base (>=4.14 && <5), bytestring (>=0.10 && <0.13), bytestring-strict-builder (>=0.4.5.4 && <0.5), comonad (>=5 && <5.1), contravariant (>=1.3 && <2), dlist (>=0.8 && <0.9 || >=1 && <2), hashable (>=1.2 && <2), hasql, iproute (>=1.7 && <1.8), mtl (>=2 && <3), postgresql-binary (>=0.15 && <0.16), postgresql-connection-string (>=0.1 && <0.2), pqi (>=0.1 && <0.2), profunctors (>=5.1 && <6), scientific (>=0.3 && <0.4), text (>=1 && <3), text-builder (>=1 && <1.1), time (>=1.9 && <2), transformers (>=0.5 && <0.7), unordered-containers (>=0.2 && <0.3), uuid (>=1.3 && <2), vector (>=0.10 && <0.14), witherable (>=0.5 && <0.6) [details]
License MIT
Copyright (c) 2014, Nikita Volkov
Author Nikita Volkov <nikita.y.volkov@mail.ru>
Maintainer Nikita Volkov <nikita.y.volkov@mail.ru>
Category Hasql, Database, PostgreSQL
Home page https://github.com/nikita-volkov/hasql
Bug tracker https://github.com/nikita-volkov/hasql/issues
Source repo head: git clone https://github.com/nikita-volkov/hasql
Uploaded by NikitaVolkov at 2026-08-05T09:34:44Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for hasql-2.0.0.0

[back to package description]

Hasql

Hackage Continuous Haddock

PostgreSQL driver for Haskell, that prioritizes:

Status

Hasql is production-ready, actively maintained and the API is moderately stable. It's used by many companies and most notably by the Postgrest project.

A New Era: Pluggable Transport

Hasql's transport is now pluggable via pqi. Hasql.Connection.acquire takes an adapter explicitly:

import Pqi.Ffi qualified    -- the existing C-backed libpq transport
import Pqi.Native qualified -- alpha: pure-Haskell, no C dependency, interchangeable with Pqi.Ffi

connection <- Hasql.Connection.acquire Pqi.Ffi.adapter settings
-- or
connection <- Hasql.Connection.acquire Pqi.Native.adapter settings

pqi-native is the goal of this new era: a from-scratch, pure-Haskell implementation of the Postgres wire protocol, verified byte-for-byte against libpq via pqi-conformance. It's alpha — not yet proven at production scale — while pqi-ffi remains the stable, production-proven default. The two adapters are fully interchangeable: swapping between them is a one-line change (a different Adapter value, nothing else), so early adopters can try pqi-native today with no lock-in and no rewrite to fall back if needed. Feedback welcome on GitHub Discussions.

Ecosystem

Hasql is not just a single library, it is a granular ecosystem of composable libraries, each isolated to perform its own task and stay simple.

Want to list your package or correct something here? Make a PR.

Why make it an ecosystem?

Tutorials

Videos

There's several videos on Hasql done as part of a nice intro-level series of live Haskell+Bazel coding by the "Ants Are Everywhere" YouTube channel:

Articles

Short Example

Following is a complete application, which performs some arithmetic in Postgres using Hasql.

{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}

import Data.Functor.Contravariant
import Data.Int
import Hasql.Session (Session)
import Prelude
import qualified Hasql.Connection as Connection
import qualified Hasql.Connection.Settings as Settings
import qualified Hasql.Decoders as Decoders
import qualified Hasql.Encoders as Encoders
import qualified Hasql.Session as Session
import qualified Hasql.Statement as Statement
import qualified Pqi.Ffi -- from "pqi-ffi" (stable); swap for "Pqi.Native" from "pqi-native" (alpha, fully interchangeable) to try the pure-Haskell backend

main :: IO ()
main = do
  Right connection <- Connection.acquire Pqi.Ffi.adapter connectionSettings
  result <- Connection.use connection (sumAndDivModSession 3 8 3)
  print result
  where
    connectionSettings =
      mconcat
        [ Settings.hostAndPort "localhost" 5432,
          Settings.user "postgres",
          Settings.password "postgres",
          Settings.dbname "postgres"
          -- Prepared statements are enabled by default.
          -- To disable them (e.g., for pgbouncer compatibility):
          -- Settings.noPreparedStatements True
        ]

-- * Sessions

-- Session abstracts over the execution of operations on a database connection.
-- It is composable and has a Monad instance.
-------------------------

sumAndDivModSession :: Int64 -> Int64 -> Int64 -> Session (Int64, Int64)
sumAndDivModSession a b c = do
  -- Get the sum of a and b
  sumOfAAndB <- Session.statement (a, b) sumStatement
  -- Divide the sum by c and get the modulo as well
  Session.statement (sumOfAAndB, c) divModStatement

-- * Statements

-- Statement is a definition of an individual SQL-statement,
-- accompanied by a specification of how to encode its parameters and
-- decode its result.
-------------------------

-- | A statement with two integer parameters and an integer result.
sumStatement :: Statement.Statement (Int64, Int64) Int64
sumStatement = Statement.preparable sql encoder decoder
  where
    -- The SQL of the statement, with $1, $2, ... placeholders for parameters.
    sql =
      "select $1 + $2"
    -- Specification of how to encode the parameters of the statement
    -- where the association with placeholders is achieved by order.
    encoder =
      mconcat
        [ -- Encoder of the first parameter as a non-nullable int8.
          -- It extracts the first element of the tuple using the contravariant functor
          -- instance.
          fst >$< Encoders.param (Encoders.nonNullable Encoders.int8),
          -- Encoder of the second parameter,
          -- which extracts the second element of the tuple.
          snd >$< Encoders.param (Encoders.nonNullable Encoders.int8)
        ]
    -- Specification of how to decode the result of the statement.
    -- States that we expect a single row with a single non-nullable int8 column.
    decoder =
      Decoders.singleRow
        (Decoders.column (Decoders.nonNullable Decoders.int8))

divModStatement :: Statement.Statement (Int64, Int64) (Int64, Int64)
divModStatement = Statement.preparable sql encoder decoder
  where
    sql =
      "select $1 / $2, $1 % $2"
    encoder =
      mconcat
        [ fst >$< Encoders.param (Encoders.nonNullable Encoders.int8),
          snd >$< Encoders.param (Encoders.nonNullable Encoders.int8)
        ]
    -- Decoder that expects a single row with two non-nullable int8 columns,
    -- returning the result as a tuple.
    -- Uses the applicative functor instance to combine two column decoders.
    decoder =
      Decoders.singleRow
        ( (,)
            <$> Decoders.column (Decoders.nonNullable Decoders.int8)
            <*> Decoders.column (Decoders.nonNullable Decoders.int8)
        )

For the general use-case it is advised to prefer declaring statements using the "hasql-th" library, which validates the statements at compile-time and generates codecs automatically. So the above two statements could be implemented the following way:

import qualified Hasql.TH as TH -- from "hasql-th"

sumStatement :: Statement.Statement (Int64, Int64) Int64
sumStatement =
  [TH.singletonStatement|
    select ($1 :: int8 + $2 :: int8) :: int8
  |]

divModStatement :: Statement.Statement (Int64, Int64) (Int64, Int64)
divModStatement =
  [TH.singletonStatement|
    select
      (($1 :: int8) / ($2 :: int8)) :: int8,
      (($1 :: int8) % ($2 :: int8)) :: int8
  |]

Discussions

Join GitHub Discussions to ask questions, provide feedback, suggest and vote on features, and help shape the future of Hasql.

Support Policy

This policy is intended to balance stability for users with the ability to evolve the library.

Each major release of Hasql is supported for at least one year from the date of its first release. During this period, fixes are backported to the latest minor version of that major release.

After the support period ends, the release may continue to work but is no longer guaranteed to receive fixes.

You're welcome to post requests to change the policy or issues if you believe something is not being addressed.