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