postgresql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. * PostgreSQL and Mono
  2. When it comes to Mono and PostgreSQL, there are many ways
  3. you can access your data.
  4. * Data Providers
  5. There are many ADO.NET data providers for <a href="http://www.postgresql.org/">PostgreSQL</a>:
  6. There are two providers created specifically for PostgreSQL included with Mono:
  7. <ul>
  8. <li><a href="http://gborg.postgresql.org/project/npgsql/projdisplay.php">Npgsql</a>
  9. <ul>
  10. <li>included with Mono</li>
  11. <li>a .NET Managed Data Provider for PostgreSQL</li>
  12. <li>Written in 100% C#</li>
  13. <li>does not require a client library</li>
  14. <li>works on Mono and Microsoft .NET</li>
  15. <li>created by Francisco Figueiredo jr. and has many developers working on it
  16. <li>works in the SQL# (command-line and GTK# GUI versions)</li>
  17. <li>in namespace Npgsql and assembly Npgsql and is found in mcs
  18. at mcs/class/Npgsql</li>
  19. </ul>
  20. </li>
  21. <li>Mono.Data.PostgreSqlClient (DEPRECATED)
  22. <ul>
  23. <li>Deprecated in favor of Npgsql. No longer included in a release of Mono.</li>
  24. </ul>
  25. </li>
  26. <li><a href="http://sourceforge.net/projects/mysqlnet/">ByteFX.Data</a> has a provider for PostgreSQL too, but I do not know how well it works with Mono.</li>
  27. <li>There is another .NET data provider for PostgreSQL named <a href="http://sourceforge.net/projects/pgsqlclient/">PgSqlClient</a>, but I do not know if it works with Mono.</li>
  28. <li>If none of the above providers meet your needs. There is the ODBC and OLEDB providers included with Mono.</li>
  29. <li>Bugs with Mono or the data provider should be reported
  30. in Mono's Bugzilla <a href="http://bugzilla.ximian.com/">here</a>. If you
  31. do not have Bugzilla user account, it is free
  32. and easy to
  33. create one <a href="http://bugzilla.ximian.com/createaccount.cgi">here</a>.</li>
  34. </ul>
  35. ** Current Status
  36. <ul>
  37. <li>Npgsql
  38. <ul>
  39. <li>Builds and Runs on both Microsoft .NET and Mono.</li>
  40. <li>Works using SQL# (command-line and GTK# versions)</li>
  41. <li>You can send insert, update, delete queries
  42. through NpgsqlCommand.ExecuteNonQuery() method.</li>
  43. <li>You can send queries like, select count(*) from table, select version()
  44. with NpgsqlCommand.ExecuteScalar() method.</li>
  45. <li>There is logging support. (Thanks Dave Page)
  46. To use it, place code like that in your program:</li>
  47. <pre>
  48. // Enable logging.
  49. NpgsqlEventLog.Level = LogLevel.Debug; // LogLevel.
  50. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile"; // LogFile.
  51. </pre>
  52. <li>You can use Npgsql with Mono (Thanks Kristis Makris). It is not working perfectly.</li>
  53. <li>There is a winforms test suite (Thanks Dave Page).</li>
  54. <li>Clearer code in NpgsqlConnection removing *magic* numbers and constants. (Thanks Kristis Makris)</li>
  55. <li>Better support of ODBC-like ConnectionString in NpgsqlConnection (Thanks Dave Page)</li>
  56. <li>Thanks Ulrich Sprick for all discussion and ideas.</li>
  57. </ul>
  58. </li>
  59. </ul>
  60. ** Action Plan
  61. <ul>
  62. <li>More testing and fixing bugs</li>
  63. <li>Better error handling</li>
  64. <li>More Data Types to use</li>
  65. <li>Any features for Npgsql should be implemented in Npgsql's main cvs repository at
  66. gborg.postgresql.org. Most bugs should be fixed in gborg.postgresql.org's cvs.
  67. Only bugs neccessary for building and running of Npgsql on Mono can be done in Mono cvs,
  68. but once applied they should be sent to Npgsql's mailing list
  69. at gborg.postgresql.org for inclusion into cvs there. Whenever there is
  70. a release of Npgsql (determined by Francisco Figueiredo jr. or a release
  71. of Mono (determined by Miguel de Icaza), then the Npgsql source
  72. in gborg.postgresql.org's cvs will be used to update the Npgsql source in
  73. Mono's cvs.
  74. </li>
  75. <li>Add any missing functionality to Npgsql. If this funtionality works on
  76. .NET but not on Mono, implement the missing features or fix the bugs in Mono</li>
  77. <li>Npgsql has been replaced Mono.Data.PostgreSqlClient as the provider of
  78. choice to use. Mono.Data.PostgreSqlClient is deprecated and is no longer included in
  79. Mono releases. Please use Npgsql for PostgreSQL data access.</li>
  80. <li>Implement new features of PostgreSQL.</li>
  81. </ul>
  82. </ul>
  83. ** Testing Npgsql
  84. <ul>
  85. <li>Have a working mono and mcs</li>
  86. <li>Get <a href="http://gborg.postgresql.org/project/npgsql/projdisplay.php">Npgsql</a>
  87. and make sure the binary assembly Npgsql.dll is installed in the same place that the
  88. mono class libraries are located.
  89. <li>C# Example for Npgsql:
  90. <pre>
  91. using System;
  92. using System.Data;
  93. using Npgsql;
  94. public class Test
  95. {
  96. public static void Main(string[] args)
  97. {
  98. string connectionString =
  99. "Server=localhost;" +
  100. "Database=test;" +
  101. "User ID=postgres;" +
  102. "Password=fun2db;";
  103. IDbConnection dbcon;
  104. dbcon = new NpgsqlConnection(connectionString);
  105. dbcon.Open();
  106. IDbCommand dbcmd = dbcon.CreateCommand();
  107. // requires a table to be created named employee
  108. // with columns firstname and lastname
  109. // such as,
  110. // CREATE TABLE employee (
  111. // firstname varchar(32),
  112. // lastname varchar(32));
  113. string sql =
  114. "SELECT firstname, lastname " +
  115. "FROM employee";
  116. dbcmd.CommandText = sql;
  117. IDataReader reader = dbcmd.ExecuteReader();
  118. while(reader.Read()) {
  119. string FirstName = (string) reader["firstname"];
  120. string LastName = (string) reader["lastname"];
  121. Console.WriteLine("Name: " +
  122. FirstName + " " + LastName);
  123. }
  124. // clean up
  125. reader.Close();
  126. reader = null;
  127. dbcmd.Dispose();
  128. dbcmd = null;
  129. dbcon.Close();
  130. dbcon = null;
  131. }
  132. }
  133. </pre>
  134. </li>
  135. <li>Building C# Example:
  136. <ul>
  137. <li>Save the example to a file, such as, TestExample.cs</li>
  138. <pre>
  139. mcs TestExample.cs -r System.Data.dll \
  140. -r Npgsql.dll
  141. </pre>
  142. </ul>
  143. </li>
  144. <li>Running the Example:
  145. <pre>
  146. mono TestExample.exe
  147. </pre>
  148. </li>
  149. </ul>
  150. ** Installation instructions for PostgreSQL DBMS:
  151. <p><b>On Unix</b>
  152. <ul>
  153. * Read the PostgreSQL Installation Instructions
  154. at \usr\doc\postgresql-x.x.x\html\installation.html
  155. * Depending on your Unix system,
  156. PostgreSQL maybe already installed, a database user 'postgres' created,
  157. a linux user 'postgres' created and initdb ran. Or maybe not.
  158. <pre>
  159. su
  160. adduser postgres
  161. mkdir /usr/local/pgsql/data
  162. chown postgres /usr/local/pgsql/data
  163. su - postgres
  164. initdb -D /usr/local/pgsql/data
  165. postmaster -i -D /usr/local/pgsql/data
  166. createdb test
  167. psql test
  168. </pre>
  169. * Make sure you have a database user named postgres. It is best to install
  170. the PostgreSQL DBMS under linux user postgres. When you run the postmaster,
  171. run it under the user postgres as well. If this was not done, then you
  172. will need to create a user named postgres for the System.Data tests.
  173. * If you already installed PostgeSQL and you do not have a database
  174. user named postgres, then you can create user postgres using psql:
  175. <pre>
  176. psql test
  177. create user postgres with password 'fun2db';
  178. </pre>
  179. * The postmaster must be run with -i option.
  180. * In the /usr/local/pgsql/data/pg_hba.conf file, you need
  181. to have the AUTH_TYPE set to md5. You can read more on this at
  182. /usr/doc/postgresql-7.2.1/html/client-authentication.html
  183. or wherever your
  184. PostgreSQL html docs are located. See the 2nd line below,
  185. host 127.0.0.1 has an AUTH_TYPE md5 in pg_hba.conf.
  186. <pre>
  187. # TYPE DATABASE IP_ADDRESS MASK AUTH_TYPE
  188. local all trust
  189. host all 127.0.0.1 255.255.255.255 md5
  190. </pre>
  191. * If you can not find your PostgreSQL documentation locally or you
  192. did not install it, then you
  193. can get it <a href="http://www.postgresql.org/idocs/">here</a>.
  194. </ul>
  195. <b>On Windows</b>
  196. <ul>
  197. * Use the <a href="http://www.cygwin.com/">Cygwin</a> installer to
  198. install the PostgreSQL DBMS. It is
  199. found in the database category.
  200. * <p>Read the file postgres-x.x.README at /usr/doc/Cygwin and read
  201. the requirements to install PostgreSQL. Those requirements
  202. are included with cygwin except cygipc. A default installtion
  203. of cygwin does not install everything you will need, so on the
  204. safe side, just include everything when installing cygwin.
  205. * <p>The -x.x in postgres-x.x is the version of your PostgreSQL DBMS.
  206. * <p>Once Cygwin has installed the PostgreSQL DBMS on your computer,
  207. read the file FAQ_MSWIN which is available
  208. in /usr/doc/postgres-x.x
  209. * <p>Important notes from this file are:
  210. <ul>
  211. <p><b>2.</b> - Install the latest <a href="http://www.neuro.gatech.edu/users/cwilson/cygutils/cygipc/index.html">CygIPC</a> package.
  212. Cygwin includes a utility bunzip2 which can be used to unzip it. Now, change to
  213. the root directory by
  214. typing "cd /" then
  215. you can use "tar xvf cygipc.xxx.tar" to untar it
  216. in the root directory in cygwin.
  217. <p>The cygipc package contains the support to run ipc-daemon
  218. that you will need
  219. to run before you can
  220. run the PostgreSQL DBMS Server daemon (postmaster) or run
  221. initdb which initializes the PostgreSQL database.
  222. <p><b>3.</b> The Cygwin bin directory has to be placed in
  223. the path before the Windows program directories,
  224. for example, C:\cygwin\bin
  225. <p><b>My own note.</b> In the Windows control panel, I set
  226. the environment variables PATH to my cygwin /usr/local/bin,
  227. /usr/bin, and /bin. I also set my LD_LIBRARY_PATH to
  228. /usr/local/lib and /usr/lib. For example:
  229. <p>
  230. <pre>
  231. PATH=c:\cygwin\usr\local\bin;c:\cygwin\usr\bin;c:\cygwin\bin;
  232. LD_LIBRARY_PATH=c:\cygwin\usr\local\lib;c:\cygwin\usr\lib;
  233. </pre>
  234. <p><b>4.</b> Start the ipc-daemon that came with the cygipc
  235. package. There
  236. are two ways to do this: run it from the command line as:
  237. <p>
  238. <pre>
  239. ipc-daemon &
  240. </pre>
  241. <p>or you can set it up as a Windows service. See the
  242. file cygrunsrv.README at /usr/doc/Cygwin on how to do this
  243. for ipc-daemon and postmaster. Note the
  244. troubleshooting section at the end of
  245. the cygrunsrv.README file.
  246. <p>To install ipc-daemon as a service,
  247. you just have to run
  248. <p>
  249. <pre>
  250. ipc-daemon --install-as-service' (--remove-as-service)
  251. </pre>
  252. <p>and then run
  253. <pre>
  254. net start ipc-daemon
  255. </pre>
  256. </ul>
  257. <p>Read the installation.html file
  258. at /usr/doc/postgresql-x.x/html/installation.html
  259. <p>You will see in this file that you will need to
  260. run the following commands:
  261. <p>
  262. <pre>
  263. mkdir /usr/local/pgsql/data
  264. initdb -D /usr/local/pgsql/data
  265. postmaster -D /usr/local/pgsql/data
  266. createdb test
  267. psql test
  268. </pre>
  269. <p>When you need to connect to the database,
  270. you will need ipc-daemon and postmaster running. Start ipc-daemon
  271. before any of the command above. If you restart your computer, you
  272. need to start ipc-daemon and postmaster either manually or as a
  273. service.
  274. <p>psql is a command-line PostgreSQL client tool to
  275. enter and run SQL commands and queries.
  276. <p>If there is no database user named postgres, create a user named
  277. postgres with the following SQL command in the client tool psql:
  278. <p>
  279. <pre>
  280. psql test
  281. create user postgres with password 'fun2db';
  282. </pre>
  283. <p>The only reason I say this is so you can easily use the System.Data tests
  284. without having to change the database, userid, etc.
  285. </ul>