postgresql 15 KB

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