README 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. Erlang PostgreSQL Database Client
  2. * Connect
  3. {ok, C} = pgsql:connect(Host, [Username], [Password], Opts).
  4. Host - host to connect to.
  5. Username - username to connect as, defaults to $USER.
  6. Password - optional password to authenticate with.
  7. Opts - property list of extra options. Supported properties:
  8. + database
  9. + port
  10. + ssl (true | false | required)
  11. + ssl_opts (see ssl docs in OTP)
  12. + timeout (milliseconds, defaults to 5000)
  13. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  14. ok = pgsql:close(C).
  15. The timeout parameter is applied to all operations. In the case of equery
  16. this means that total execution time may exceed the timeout value.
  17. * Simple Query
  18. {ok, Columns, Rows} = pgsql:squery(C, Sql).
  19. {error, Error} = pgsql:squery(C, InvalidSql).
  20. Columns - list of column records, see pgsql.hrl for definition.
  21. Rows - list of tuples, one for each row.
  22. The simple query protocol returns all columns as text (Erlang binaries)
  23. and does not support binding parameters.
  24. * Extended Query
  25. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  26. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  27. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  28. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  29. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  30. The extended query protocol combines parse, bind, and execute using
  31. the unnamed prepared statement and portal. A "select" statement returns
  32. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  33. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  34. an error occurs, all statements result in {error, #error{}}.
  35. PostgreSQL's binary format is used to return integers as Erlang
  36. integers, floats as floats, bytea/text/varchar columns as binaries,
  37. bools as true/false, etc. For details see pgsql_binary.erl and the
  38. Data Representation section below.
  39. * Parse/Bind/Execute
  40. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  41. StatementName - optional, reusable, name for the prepared statement.
  42. ParameterTypes - optional list of PostgreSQL types for each parameter.
  43. For valid type names see pgsql_types.erl.
  44. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  45. PortalName - optional name for the result portal.
  46. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  47. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  48. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  49. PortalName - optional portal name used in bind/4.
  50. MaxRows - maximum number of rows to return (0 for all rows).
  51. execute returns {partial, Rows} when more rows are available.
  52. ok = pgsql:close(C, Statement).
  53. ok = pgsql:close(C, statement | portal, Name).
  54. ok = pgsql:sync(C).
  55. All functions return {error, Error} when an error occurs.
  56. * Data Representation
  57. null = null
  58. bool = true | false
  59. char = $A
  60. intX = 1
  61. floatX = 1.0
  62. date = {Year, Month, Day}
  63. time = {Hour, Minute, Second.Microsecond}
  64. timetz = {time, Timezone}
  65. timestamp = {date, time}
  66. timestamptz = {date, time}
  67. interval = {time, Days, Months}
  68. text = <<"a">>
  69. varchar = <<"a">>
  70. bytea = <<1, 2>>
  71. record = {int2, time, text, ...} (decode only)
  72. * Errors
  73. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  74. see pgsql.hrl for the record definition. epgsql may also return {error, Atom}
  75. where Atom is 'timeout' or 'closed'.