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