postgresql 18 KB

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