Difference between revisions of "postgres"
m (→Example: sqldbtutorial1 mentioned) |
(Removed superfluous demo (also exists in sqldb article as wel as sqldb tutorial1). Clarified error messages) |
||
Line 17: | Line 17: | ||
FPC/Lazarus supports PostgreSQL out of the box with a PostgreSQL connection component/class. | FPC/Lazarus supports PostgreSQL out of the box with a PostgreSQL connection component/class. | ||
− | + | The component doesn't support the following connection option: | |
− | + | * ClientEncoding (***September 2012: it may have been implemented in current FPC development versions) | |
− | |||
− | |||
− | |||
− | * | ||
− | * | ||
− | |||
The TPQConnection component does not directly support a Port property, but one can pass the port into the component via the Params parameter: | The TPQConnection component does not directly support a Port property, but one can pass the port into the component via the Params parameter: | ||
Line 32: | Line 26: | ||
Also other connection parameters can be specified using Params property: | Also other connection parameters can be specified using Params property: | ||
<syntaxhighlight>PQConnection.Params.Add('application_name=''yourappname''')</syntaxhighlight> | <syntaxhighlight>PQConnection.Params.Add('application_name=''yourappname''')</syntaxhighlight> | ||
− | |||
− | + | ===Example=== | |
− | + | See [[SQLdb_Tutorial1]] for a tutorial on creating a GUI database-enabled program that is written for PostgreSQL/SQLDB, as well as SQLite/SQLDB, Firebird/SQLDB, basically any RDBMS SQLDB supports). | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | === Installation and errors === | |
− | + | As with all sqldb units, you need to add your driver libraries to a directory in the (library search) path or to the project+program output directories. | |
− | |||
+ | ==== Error: "Can not load PostgreSQL client library "libpq.dll"" ==== | ||
+ | The program cannot find your PostgreSQL driver files. | ||
+ | Copy '''all''' required PostgreSQL .dll/.manifest files either | ||
+ | * to your project directory and your executable output directory, | ||
+ | * or to a directory in your search path (e.g. c:\windows\system32\) | ||
− | + | A good example that demonstrates how to include drive DLL files when connecting Lazarus with PostgreSQL under Windows is [http://paginas.fe.up.pt/~asousa/wiki/doku.php?id=proj:lazarus_projects#easy_db easyDB]. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | On Linux/Unix/OSX: make sure the PostgreSQL libraries are in your library search path, e.g.: | |
+ | * On linux add the path to the libpq.so file to the libraries section in your /etc/fpc.cfg file. For example : -Fl/usr/local/pgsql/lib | ||
+ | * It may be necessary to create a symbolic link from a specific library version to a general library name: <syntaxhighlight lang="bash">ln -s /usr/lib/pqsql.so.5 /usr/lib/pqsql.so</syntaxhighlight>. Alternatively, install the postgresql client -dev package using your distribution's package manager | ||
==PostgreSQL package: the low level units== | ==PostgreSQL package: the low level units== |
Revision as of 15:22, 29 September 2012
Overview
You can use Free Pascal/Lazarus to access a PostgreSQL database server. If you are looking for information on the mysql package in FPC, please see postgres#PostgreSQL_package:_the_low_level_units below.
Advantages of PostgreSQL:
- It is very widely used and available
- Very stable and has a complete feature set
Disadvantage of PostgreSQL:
- Some hosters may not offer PostgreSQL)
- No embedded version
Zeos
Zeos supports PostgreSQL; please see ZeosDBO
SQLDB
FPC/Lazarus supports PostgreSQL out of the box with a PostgreSQL connection component/class.
The component doesn't support the following connection option:
- ClientEncoding (***September 2012: it may have been implemented in current FPC development versions)
The TPQConnection component does not directly support a Port property, but one can pass the port into the component via the Params parameter:
PQConnection.Params.Add('port=' + VariableContainingPort);
Also other connection parameters can be specified using Params property:
PQConnection.Params.Add('application_name=''yourappname''')
Example
See SQLdb_Tutorial1 for a tutorial on creating a GUI database-enabled program that is written for PostgreSQL/SQLDB, as well as SQLite/SQLDB, Firebird/SQLDB, basically any RDBMS SQLDB supports).
Installation and errors
As with all sqldb units, you need to add your driver libraries to a directory in the (library search) path or to the project+program output directories.
Error: "Can not load PostgreSQL client library "libpq.dll""
The program cannot find your PostgreSQL driver files. Copy all required PostgreSQL .dll/.manifest files either
- to your project directory and your executable output directory,
- or to a directory in your search path (e.g. c:\windows\system32\)
A good example that demonstrates how to include drive DLL files when connecting Lazarus with PostgreSQL under Windows is easyDB.
On Linux/Unix/OSX: make sure the PostgreSQL libraries are in your library search path, e.g.:
- On linux add the path to the libpq.so file to the libraries section in your /etc/fpc.cfg file. For example : -Fl/usr/local/pgsql/lib
- It may be necessary to create a symbolic link from a specific library version to a general library name: . Alternatively, install the postgresql client -dev package using your distribution's package manager
ln -s /usr/lib/pqsql.so.5 /usr/lib/pqsql.so
PostgreSQL package: the low level units
As with all databases, the SQLDB code depends on a lower level PostgreSQL specific unit that wraps around the PostgreSQL driver library (.so/.dll). Normally, you would use the higher-level SQLDB code as it allows you to code more quickly, easily switch databases etc.
Using this is very easy, all you need to do is compile some units, and use these units in your program. You need to specify the place of the PostgreSQL client Library (libpq) when compiling, and that is it.
The main unit is called postgres, normally this is the only unit you must include in your uses clause.
Requirements
You need at least version 0.99.5 of Free Pascal (basically any version of FPC except extremely old ones). The headers are translated from PostGreSQL version 6.3.1.
Installation
The prostgres unit comes with the Free Pascal packages, and is distributed together with the compiler. This contains a directory postgres with the units, a test program and a makefile. cd to the directory and edit the Makefile to set the variables for your system. You must provide only 1 thing:
- The directory where the libpq library resides, usually /usr/local/pgsql/lib
Typing
make
Should compile the units and the program. If compilation was succesfull, you can install with
make install
(Remember to set the directory where the units should be installed.)
You can then test the program by running
make test
This will:
- Run the test program testpg. It is a straightforward pascal translation of the example program in the PostGreSQL programmers' guide.
- Run a script to create a table in a database, and fill it with some data. (the psql program should be in your PATH for this) . By default, the used database is testdb.
- Run the testprogram testemail
- Run a shell script again to remove the created table.
You will see a lot of messages on your screen, giving you feedback and results. If something went wrong, make will inform you of this.
Go back to Packages List
See also
- Lazarus DB Faq - More about database programming
- Lazarus Database Tutorial - Getting Lazarus working with various databases