README 5.3 KB

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