postgresql 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. * PostgreSQL and Mono
  2. When it comes to Mono and PostgreSQL, there are many ways
  3. you can handle your data. You have many Mono Data Providers which can be used
  4. to access data from a application written for Mono.
  5. Then there is the future goal of having the ability to host Mono within PostgreSQL to
  6. have the applications run on the server which makes things much faster.
  7. * Hosting Mono in PostgreSQL
  8. There is a project to <a href="http://gborg.postgresql.org/project/plmono/projdisplay.php">host Mono within PostgreSQL</a>.
  9. plMono is a PostgreSQL language using the embedded Mono runtime. It provides support for writing functions in C#, or any other language that supports .NET.
  10. * Data Providers
  11. There are many ADO.NET data providers for <a href="http://www.postgresql.org/">PostgreSQL</a>:
  12. There are two providers created specifically for PostgreSQL included with Mono:
  13. <ul>
  14. <li><a href="http://gborg.postgresql.org/project/npgsql/projdisplay.php">Npgsql</a>
  15. <ul>
  16. <li>included with Mono</li>
  17. <li>a .NET Managed Data Provider for PostgreSQL</li>
  18. <li>Written in 100% C#</li>
  19. <li>does not require a client library</li>
  20. <li>works on Mono and Microsoft .NET</li>
  21. <li>created by Francisco Figueiredo jr. and has many developers working on it
  22. <li>works in the SQL# (command-line and GTK# GUI versions)</li>
  23. <li>in namespace Npgsql and assembly Npgsql and is found in mcs
  24. at mcs/class/Npgsql</li>
  25. </ul>
  26. </li>
  27. <li>Mono.Data.PostgreSQL (deprecated)
  28. <ul>
  29. <li>included with Mono</li>
  30. <li>Deprecated in favor of Npgsql</li>
  31. <li>Exists in namespace Mono.Data.PostgreSql and assembly Mono.Data.PostgreSql</li>
  32. <li>Is a Mono Data Provider for the <a href="http://www.postgresql.org/">PostgreSQL</a>
  33. client/server database management system.</li>
  34. <li>Written in C# and has C# bindings to the PostgreSQL C Client library pq.dll on Windows
  35. and libpq.so on Linux.</li>
  36. </ul>
  37. </li>
  38. <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>
  39. <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>
  40. <li>If none of the above providers meet your needs. There is the ODBC and OLEDB providers included with Mono.</li>
  41. <li>Bugs with Mono or the data provider should be reported
  42. in Mono's Bugzilla <a href="http://bugzilla.ximian.com/">here</a>. If you
  43. do not have Bugzilla user account, it is free
  44. and easy to
  45. create one <a href="http://bugzilla.ximian.com/createaccount.cgi">here</a>.</li>
  46. </ul>
  47. Below, see separate Testing sections for Npgsql and Mono.Data.PostgreSqlClient.
  48. ** Current Status
  49. <ul>
  50. <li>Npgsql
  51. <ul>
  52. <li>Builds and Runs on both Microsoft .NET and Mono.</li>
  53. <li>Works using SQL# (command-line and GTK# versions)</li>
  54. <li>You can send insert, update, delete queries
  55. through NpgsqlCommand.ExecuteNonQuery() method.</li>
  56. <li>You can send queries like, select count(*) from table, select version()
  57. with NpgsqlCommand.ExecuteScalar() method.</li>
  58. <li>There is logging support. (Thanks Dave Page)
  59. To use it, place code like that in your program:</li>
  60. <pre>
  61. // Enable logging.
  62. NpgsqlEventLog.Level = LogLevel.Debug; // LogLevel.
  63. NpgsqlEventLog.LogName = "NpgsqlTests.LogFile"; // LogFile.
  64. </pre>
  65. <li>You can use Npgsql with Mono (Thanks Kristis Makris). It is not working perfectly.</li>
  66. <li>There is a winforms test suite (Thanks Dave Page).</li>
  67. <li>Clearer code in NpgsqlConnection removing *magic* numbers and constants. (Thanks Kristis Makris)</li>
  68. <li>Better support of ODBC-like ConnectionString in NpgsqlConnection (Thanks Dave Page)</li>
  69. <li>Thanks Ulrich Sprick for all discussion and ideas.</li>
  70. </ul>
  71. </li>
  72. <li>Mono.Data.PostgreSQL status
  73. <ul>
  74. <li>Deprecated in favor of Npgsql</li>
  75. <li>We are able to do simple CREATE TABLE, DROP TABLE, UPDATE, INSERT, and
  76. DELETE SQL commands using the ExecuteNonQuery method in PgSqlCommand.</li>
  77. <li>We can execute multiple queries and do a NextResult() in PgSqlDataReader()
  78. to get the next result set.</li>
  79. <li>We are also able to do simple aggregate functions,
  80. ie, count(), sum(), min(), and max()
  81. in a simple SELECT SQL query using the ExecuteScalar() now.</li>
  82. <li>We are also able to retrieve data with a simple SELECT SQL query
  83. using ExecuteReader() which returns a PgSqlDataReader. We are able to
  84. use GetSchemaTable() to get the meta data about the table columns.
  85. We are able to Read() to get each row from the result set.</li>
  86. <li>We are able to get
  87. String data (char, character, text, varchar), Int16 (smallint),
  88. Int32 (integer), Int64 (bigint), DateTime (time, date, timestamp),
  89. Boolean (boolean), Single (float), and Double (double).
  90. More data types will come later. Note, the types that do work still
  91. need thorough testing.</li>
  92. <li>Rows that are returned which contain columns that are NULL are handled now.
  93. The PgSqlDataReader method IsDBNull() needs to be called to determine
  94. if a field IS NULL before trying to read data from that field.</li>
  95. <li>Calling PostgreSQL stored procedures works. It does not work perfectly.
  96. It may not
  97. even work to specification - yet. If you want to test it yourself, look at
  98. TestSqlDataReader.cs or PostgresTest.cs in
  99. mcs/class/System.Data/Test.</li>
  100. <li>Below, I have some sample code you can
  101. use to call a PostgreSQL stored procedure named "version". This stored
  102. procedure returns a string containing the PostgreSQL server version. Notice
  103. the CommandType is StoredProcedure and the method ExecuteScalar() is called.</li>
  104. <li>ExecuteScalar() is a lightweight method in class PgSqlCommand that only returns
  105. one row and one column as one object - even if there is more than row or column.</li>
  106. <li>We have the beginnings of Parameters support PostgreSQL. Only
  107. Input Parameters are currently supported. Output, Input/Output,
  108. and Return parameters still need to be done.</li>
  109. <li>A lot of Exceptions need to be thrown for various exceptions. However,
  110. PgSqlException, PgSqlErrorCollection, and PgSqlError have been partially
  111. implemented.</li>
  112. <li>Tim Coleman and Rodrigo Moya got the beginnings of the
  113. PgSqlDataAdapter/DataSet/DataTable/DataRow to work. Currently,
  114. the PgSqlDataAdapter can Fill() relational data into a DataTable in a DataSet.
  115. See the test mcs/class/System.Data/Test/TestSqlDataAdapter.cs to see it in action.</li>
  116. <li>Works in the SQL# command-line version
  117. and the GTK# version on Linux. It only works in SQL# command-line version
  118. on Windows.</li>
  119. </ul>
  120. </li>
  121. </ul>
  122. ** Action Plan
  123. <ul>
  124. <li>More testing and fixing bugs</li>
  125. <li>Better error handling</li>
  126. <li>More Data Types to use</li>
  127. <li>Any features for Npgsql should be implemented in Npgsql's main cvs repository at
  128. gborg.postgresql.org. Most bugs should be fixed in gborg.postgresql.org's cvs.
  129. Only bugs neccessary for building and running of Npgsql on Mono can be done in Mono cvs,
  130. but once applied they should be sent to Npgsql's mailing list
  131. at gborg.postgresql.org for inclusion into cvs there. Whenever there is
  132. a release of Npgsql (determined by Francisco Figueiredo jr. or a release
  133. of Mono (determined by Miguel de Icaza), then the Npgsql source
  134. in gborg.postgresql.org's cvs will be used to update the Npgsql source in
  135. Mono's cvs.
  136. </li>
  137. <li>Mono.Data.PostgreSqlClient even though deprecated can still
  138. accept bug fixes. This is because other areas, such as, ASP.NET examples
  139. may still use this provider.</li>
  140. <li>Add any missing functionality to Npgsql. If this funtionality works on
  141. .NET but not on Mono, implement the missing features or fix the bugs in Mono</li>
  142. <li>Npgsql has replaced Mono.Data.PostgreSqlClient as the provider of
  143. choice to use. However, Mono.Data.PostgreSqlClient will remain in a
  144. deprecated state until nobody uses it anymore - then it can be removed</li>
  145. <li>Implement more of PostgreSQL 7.3 features in Npgsql</li>
  146. </ul>
  147. ** Testing Mono.Data.PostgreSqlClient
  148. <ul>
  149. * <p>In order to test Mono.Data.PostgreSqlClient, you will need to have
  150. access to a remote PostgreSQL DBMS, or you will have to install
  151. one locally. PostgreSQL was the first ADO.NET provider created in Mono.
  152. <p>Why use PostgreSQL? Because it is free software, has a client
  153. library that is easy to use, PostgreSQL is easy to install on
  154. Unix and Windows (using the Cygwin install program), not difficult to setup after
  155. installation, and it runs under: Linux,
  156. Windows (via cygwin and ipc-daemon), Unix, and
  157. others. This allowed us to create the
  158. System.Data functionality in Mono much quicker.
  159. <p>If you plan on using a remote PostgreSQL DBMS Server,
  160. than you will need to have the PostgreSQL client software on your
  161. local computer that includes libpq.so (pq.dll on Windows).
  162. <p>The System.Data tests use this connection string to connect
  163. to the PostgreSQL database named "test" at host "localhost" as
  164. user "postgres".
  165. <pre>
  166. "Server=localhost;Database=test;User ID=postgres;Password=fun2db"
  167. (or)
  168. "host=localhost;dbname=test;user=postgres;password=fun2db"
  169. </pre>
  170. </ul>
  171. <p>Installation instructions for PostgreSQL DBMS:
  172. <b>On Unix</b>
  173. <ul>
  174. * Read the PostgreSQL Installation Instructions
  175. at \usr\doc\postgresql-x.x.x\html\installation.html
  176. * Depending on your Unix system,
  177. PostgreSQL maybe already installed, a database user 'postgres' created,
  178. a linux user 'postgres' created and initdb ran. Or maybe not.
  179. <pre>
  180. su
  181. adduser postgres
  182. mkdir /usr/local/pgsql/data
  183. chown postgres /usr/local/pgsql/data
  184. su - postgres
  185. initdb -D /usr/local/pgsql/data
  186. postmaster -i -D /usr/local/pgsql/data
  187. createdb test
  188. psql test
  189. </pre>
  190. * Make sure you have a database user named postgres. It is best to install
  191. the PostgreSQL DBMS under linux user postgres. When you run the postmaster,
  192. run it under the user postgres as well. If this was not done, then you
  193. will need to create a user named postgres for the System.Data tests.
  194. * If you already installed PostgeSQL and you do not have a database
  195. user named postgres, then you can create user postgres using psql:
  196. <pre>
  197. psql test
  198. create user postgres with password 'fun2db';
  199. </pre>
  200. * The postmaster must be run with -i option.
  201. * In the /usr/local/pgsql/data/pg_hba.conf file, you need
  202. to have the AUTH_TYPE set to md5. You can read more on this at
  203. /usr/doc/postgresql-7.2.1/html/client-authentication.html
  204. or wherever your
  205. PostgreSQL html docs are located. See the 2nd line below,
  206. host 127.0.0.1 has an AUTH_TYPE md5 in pg_hba.conf.
  207. <pre>
  208. # TYPE DATABASE IP_ADDRESS MASK AUTH_TYPE
  209. local all trust
  210. host all 127.0.0.1 255.255.255.255 md5
  211. </pre>
  212. * If you can not find your PostgreSQL documentation locally or you
  213. did not install it, then you
  214. can get it <a href="http://www.postgresql.org/idocs/">here</a>.
  215. </ul>
  216. <b>On Windows</b>
  217. <ul>
  218. * Use the <a href="http://www.cygwin.com/">Cygwin</a> installer to
  219. install the PostgreSQL DBMS. It is
  220. found in the database category.
  221. * <p>Read the file postgres-x.x.README at /usr/doc/Cygwin and read
  222. the requirements to install PostgreSQL. Those requirements
  223. are included with cygwin except cygipc. A default installtion
  224. of cygwin does not install everything you will need, so on the
  225. safe side, just include everything when installing cygwin.
  226. * <p>The -x.x in postgres-x.x is the version of your PostgreSQL DBMS.
  227. * <p>Once Cygwin has installed the PostgreSQL DBMS on your computer,
  228. read the file FAQ_MSWIN which is available
  229. in /usr/doc/postgres-x.x
  230. * <p>Important notes from this file are:
  231. <ul>
  232. <p><b>2.</b> - Install the latest <a href="http://www.neuro.gatech.edu/users/cwilson/cygutils/cygipc/index.html">CygIPC</a> package.
  233. Cygwin includes a utility bunzip2 which can be used to unzip it. Now, change to
  234. the root directory by
  235. typing "cd /" then
  236. you can use "tar xvf cygipc.xxx.tar" to untar it
  237. in the root directory in cygwin.
  238. <p>The cygipc package contains the support to run ipc-daemon
  239. that you will need
  240. to run before you can
  241. run the PostgreSQL DBMS Server daemon (postmaster) or run
  242. initdb which initializes the PostgreSQL database.
  243. <p><b>3.</b> The Cygwin bin directory has to be placed in
  244. the path before the Windows program directories,
  245. for example, C:\cygwin\bin
  246. <p><b>My own note.</b> In the Windows control panel, I set
  247. the environment variables PATH to my cygwin /usr/local/bin,
  248. /usr/bin, and /bin. I also set my LD_LIBRARY_PATH to
  249. /usr/local/lib and /usr/lib. For example:
  250. <p>
  251. <pre>
  252. PATH=c:\cygwin\usr\local\bin;c:\cygwin\usr\bin;c:\cygwin\bin;
  253. LD_LIBRARY_PATH=c:\cygwin\usr\local\lib;c:\cygwin\usr\lib;
  254. </pre>
  255. <p><b>4.</b> Start the ipc-daemon that came with the cygipc
  256. package. There
  257. are two ways to do this: run it from the command line as:
  258. <p>
  259. <pre>
  260. ipc-daemon &
  261. </pre>
  262. <p>or you can set it up as a Windows service. See the
  263. file cygrunsrv.README at /usr/doc/Cygwin on how to do this
  264. for ipc-daemon and postmaster. Note the
  265. troubleshooting section at the end of
  266. the cygrunsrv.README file.
  267. <p>To install ipc-daemon as a service,
  268. you just have to run
  269. <p>
  270. <pre>
  271. ipc-daemon --install-as-service' (--remove-as-service)
  272. </pre>
  273. <p>and then run
  274. <pre>
  275. net start ipc-daemon
  276. </pre>
  277. </ul>
  278. <p>Read the installation.html file
  279. at /usr/doc/postgresql-x.x/html/installation.html
  280. <p>You will see in this file that you will need to
  281. run the following commands:
  282. <p>
  283. <pre>
  284. mkdir /usr/local/pgsql/data
  285. initdb -D /usr/local/pgsql/data
  286. postmaster -D /usr/local/pgsql/data
  287. createdb test
  288. psql test
  289. </pre>
  290. <p>When you need to connect to the database,
  291. you will need ipc-daemon and postmaster running. Start ipc-daemon
  292. before any of the command above. If you restart your computer, you
  293. need to start ipc-daemon and postmaster either manually or as a
  294. service.
  295. <p>psql is a command-line PostgreSQL client tool to
  296. enter and run SQL commands and queries.
  297. <p>If there is no database user named postgres, create a user named
  298. postgres with the following SQL command in the client tool psql:
  299. <p>
  300. <pre>
  301. psql test
  302. create user postgres with password 'fun2db';
  303. </pre>
  304. <p>The only reason I say this is so you can easily use the System.Data tests
  305. without having to change the database, userid, etc.
  306. </ul>
  307. <p>In the path mcs/class/System.Data/Test
  308. there is a test for Mono.Data.PostgreSqlClient named
  309. PostgreTest.cs. Thanks goes to Gonzalo for creating the original
  310. PostgreSQL test.
  311. <p>
  312. To compile the PostgresTest.cs program, do:
  313. <p>
  314. <pre>
  315. mcs PostgresTest.cs \
  316. -r System.Data.dll \
  317. -r Mono.Data.PostgreSqlClient.dll
  318. </pre>
  319. <p>If there are compile errors, such as, can not convert IDbConnection
  320. to PgSqlConnection, then you need to run mcs like:
  321. <pre>
  322. mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe \
  323. PostgresTest.cs \
  324. -r System.Data.dll \
  325. -r Mono.Data.PostgreSqlClient.dll
  326. </pre>
  327. <p>
  328. To run using mint, do:
  329. <p>
  330. <pre>
  331. mint PostgresTest.exe
  332. </pre>
  333. <p>
  334. To run using mono, do:
  335. <pre>
  336. mono PostgresTest.exe
  337. </pre>
  338. <p>C# Example for Mono.Data.PostgreSqlClient:
  339. <pre>
  340. using System;
  341. using System.Data;
  342. using Mono.Data.PostgreSqlClient;
  343. public class Test
  344. {
  345. public static void Main(string[] args)
  346. {
  347. string connectionString =
  348. "Server=localhost;" +
  349. "Database=test;" +
  350. "User ID=postgres;" +
  351. "Password=fun2db;";
  352. IDbConnection dbcon;
  353. dbcon = new PgConnection(connectionString);
  354. dbcon.Open();
  355. IDbCommand dbcmd = dbcon.CreateCommand();
  356. // requires a table to be created named employee
  357. // with columns firstname and lastname
  358. // such as,
  359. // CREATE TABLE employee (
  360. // firstname varchar(32),
  361. // lastname varchar(32));
  362. string sql =
  363. "SELECT firstname, lastname" +
  364. "FROM employee";
  365. dbcmd.CommandText = sql;
  366. IDataReader reader = dbcmd.ExecuteReader();
  367. while(reader.Read()) {
  368. string FirstName = (string) reader["firstname"];
  369. string LastName = (string) reader["lastname"];
  370. Console.WriteLine("Name: " +
  371. FirstName + " " + LastName);
  372. }
  373. // clean up
  374. reader.Close();
  375. reader = null;
  376. dbcmd.Dispose();
  377. dbcmd = null;
  378. dbcon.Close();
  379. dbcon = null;
  380. }
  381. }
  382. </pre>
  383. </li>
  384. <li>Building C# Example:
  385. <ul>
  386. <li>Save the example to a file, such as, TestExample.cs</li>
  387. <li>Build on Linux:
  388. <pre>
  389. mcs TestExample.cs -r System.Data.dll \
  390. -r Mono.Data.PostgreSqlClient.dll
  391. </pre>
  392. </li>
  393. <li>Build on Windows via Cygwin:
  394. <pre>
  395. mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe \
  396. TestExample.cs \
  397. -lib:C:/cygwin/home/MyHome/mono/install/lib \
  398. -r System.Data.dll -r Mono.Data.PostgreSqlClient.dll
  399. </pre>
  400. </li>
  401. </ul>
  402. </li>
  403. <li>Running the Example:
  404. <pre>
  405. mono TestExample.exe
  406. </pre>
  407. </li>
  408. </ul>
  409. ** Testing Npgsql
  410. <ul>
  411. <li>Have a working mono and mcs</li>
  412. <li>Get <a href="http://gborg.postgresql.org/project/npgsql/projdisplay.php">Npgsql</a>
  413. and make sure the binary assembly Npgsql.dll is installed in the same place that the
  414. mono class libraries are located.
  415. <li>Read the Testing notes for Mono.Data.PostgreSqlClient too
  416. <li>C# Example for Npgsql:
  417. <pre>
  418. using System;
  419. using System.Data;
  420. using Npgsql;
  421. public class Test
  422. {
  423. public static void Main(string[] args)
  424. {
  425. string connectionString =
  426. "Server=localhost;" +
  427. "Database=test;" +
  428. "User ID=postgres;" +
  429. "Password=fun2db;";
  430. IDbConnection dbcon;
  431. dbcon = new NpgsqlConnection(connectionString);
  432. dbcon.Open();
  433. IDbCommand dbcmd = dbcon.CreateCommand();
  434. // requires a table to be created named employee
  435. // with columns firstname and lastname
  436. // such as,
  437. // CREATE TABLE employee (
  438. // firstname varchar(32),
  439. // lastname varchar(32));
  440. string sql =
  441. "SELECT firstname, lastname " +
  442. "FROM employee";
  443. dbcmd.CommandText = sql;
  444. IDataReader reader = dbcmd.ExecuteReader();
  445. while(reader.Read()) {
  446. string FirstName = (string) reader["firstname"];
  447. string LastName = (string) reader["lastname"];
  448. Console.WriteLine("Name: " +
  449. FirstName + " " + LastName);
  450. }
  451. // clean up
  452. reader.Close();
  453. reader = null;
  454. dbcmd.Dispose();
  455. dbcmd = null;
  456. dbcon.Close();
  457. dbcon = null;
  458. }
  459. }
  460. </pre>
  461. </li>
  462. <li>Building C# Example:
  463. <ul>
  464. <li>Save the example to a file, such as, TestExample.cs</li>
  465. <li>Build on Linux:
  466. <pre>
  467. mcs TestExample.cs -r System.Data.dll \
  468. -r Npgsql.dll
  469. </pre>
  470. </li>
  471. <li>Build on Windows via Cygwin:
  472. <pre>
  473. mono C:/cygwin/home/MyHome/mono/install/bin/mcs.exe \
  474. TestExample.cs \
  475. -lib:C:/cygwin/home/MyHome/mono/install/lib \
  476. -r System.Data.dll -r Npgsql.dll
  477. </pre>
  478. </li>
  479. </ul>
  480. </li>
  481. <li>Running the Example:
  482. <pre>
  483. mono TestExample.exe
  484. </pre>
  485. </li>
  486. </ul>