README 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, String}
  9. + {port, Integer}
  10. + {ssl, Atom} true | false | required
  11. + {ssl_opts, List} see ssl application docs in OTP
  12. + {timeout, Integer} milliseconds, defaults to 5000
  13. + {async, Pid} see Asynchronous Messages section
  14. {ok, C} = pgsql:connect("localhost", "username", [{database, "test_db"}]).
  15. ok = pgsql:close(C).
  16. The timeout parameter will trigger an {error, timeout} result when the
  17. server fails to respond within Timeout milliseconds.
  18. * Simple Query
  19. {ok, Columns, Rows} = pgsql:squery(C, "select ...").
  20. {ok, Count} = pgsql:squery(C, "update ...").
  21. {ok, Count, Columns, Rows} = pgsql:squery(C, "insert ... returning ...").
  22. {error, Error} = pgsql:squery(C, "invalid SQL").
  23. Columns - list of column records, see pgsql.hrl for definition.
  24. Rows - list of tuples, one for each row.
  25. Count - integer count of rows inserted/updated/etc
  26. The simple query protocol returns all columns as text (Erlang binaries)
  27. and does not support binding parameters.
  28. * Extended Query
  29. {ok, Columns, Rows} = pgsql:equery(C, "select ...", [Parameters]).
  30. {ok, Count} = pgsql:equery(C, "update ...", [Parameters]).
  31. {ok, Count, Columns, Rows} = pgsql:equery(C, "insert ... returning ...", [Parameters]).
  32. {error, Error} = pgsql:equery(C, "invalid SQL", [Parameters]).
  33. Parameters - optional list of values to be bound to $1, $2, $3, etc.
  34. The extended query protocol combines parse, bind, and execute using
  35. the unnamed prepared statement and portal. A "select" statement returns
  36. {ok, Columns, Rows}, "insert/update/delete" returns {ok, Count} or
  37. {ok, Count, Columns, Rows} when a "returning" clause is present. When
  38. an error occurs, all statements result in {error, #error{}}.
  39. PostgreSQL's binary format is used to return integers as Erlang
  40. integers, floats as floats, bytea/text/varchar columns as binaries,
  41. bools as true/false, etc. For details see pgsql_binary.erl and the
  42. Data Representation section below.
  43. * Parse/Bind/Execute
  44. {ok, Statement} = pgsql:parse(C, [StatementName], Sql, [ParameterTypes]).
  45. StatementName - optional, reusable, name for the prepared statement.
  46. ParameterTypes - optional list of PostgreSQL types for each parameter.
  47. For valid type names see pgsql_types.erl.
  48. ok = pgsql:bind(C, Statement, [PortalName], ParameterValues).
  49. PortalName - optional name for the result portal.
  50. {ok | partial, Rows} = pgsql:execute(C, Statement, [PortalName], [MaxRows]).
  51. {ok, Count} = pgsql:execute(C, Statement, [PortalName]).
  52. {ok, Count, Rows} = pgsql:execute(C, Statement, [PortalName]).
  53. PortalName - optional portal name used in bind/4.
  54. MaxRows - maximum number of rows to return (0 for all rows).
  55. execute returns {partial, Rows} when more rows are available.
  56. ok = pgsql:close(C, Statement).
  57. ok = pgsql:close(C, statement | portal, Name).
  58. ok = pgsql:sync(C).
  59. All functions return {error, Error} when an error occurs.
  60. * Data Representation
  61. null = null
  62. bool = true | false
  63. char = $A | binary
  64. intX = 1
  65. floatX = 1.0
  66. date = {Year, Month, Day}
  67. time = {Hour, Minute, Second.Microsecond}
  68. timetz = {time, Timezone}
  69. timestamp = {date, time}
  70. timestamptz = {date, time}
  71. interval = {time, Days, Months}
  72. text = <<"a">>
  73. varchar = <<"a">>
  74. bytea = <<1, 2>>
  75. record = {int2, time, text, ...} (decode only)
  76. * Errors
  77. Errors originating from the PostgreSQL backend are returned as {error, #error{}},
  78. see pgsql.hrl for the record definition. epgsql functions may also return
  79. {error, What} where What is one of the following:
  80. {unsupported_auth_method, Method} - required auth method is unsupported
  81. timeout - request timed out
  82. closed - connection was closed
  83. sync_required - error occured and pgsql:sync must be called
  84. * Asynchronous Messages
  85. PostgreSQL may deliver two types of asynchronous message: "notices" in response
  86. to notice and warning messages generated by the server, and "notifications" which
  87. are generated by the LISTEN/NOTIFY mechanism.
  88. Passing the {async, Pid} option to pgsql:connect will result in these async
  89. messages being sent to the specified process, otherwise they will be dropped.
  90. Message formats:
  91. {pgsql, Connection, {notification, Channel, Pid, Payload}}
  92. Connection - connection the notification occured on
  93. Channel - channel the notification occured on
  94. Pid - database session pid that sent notification
  95. Payload - optional payload, only available from PostgreSQL >= 9.0
  96. {pgsql, Connection, {notice, Error}}
  97. Connection - connection the notice occured on
  98. Error - an #error{} record, see pgsql.hrl