Notes about PostgreSQL

From OpenPetra Wiki
Revision as of 21:25, 12 March 2013 by Dwmosman (talk | contribs) (Add instructions on OpenPetra configuration for PostgreSQL)
Jump to navigation Jump to search

Installation

Windows

Running the Installer

  • You might choose to not install a Windows Service for running the PostgreSQL Server, if that option is present in the installer. (It is a personal choice whether you want to run PostgreSQL as a Service or from the Command Line.)


Initialising the DB Cluster and starting the DB Server

You might want to set the environment variables before creating the database:

set PGUSER=postgres
set PGPASSWORD=myPassword

Or alternatively, write the file pgpass.conf; see http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html


From the Postgres program folder (e.g. C:\Program Files\PostgreSQL\9.0\bin\) run the following separate commands in a command window:

initdb -D "petra" <ENTER> 

This will create a new Database Cluster called 'petra' (for details on what this is for see initdb command in the PostgreSQL Documentation).

postgres -D "petra" <ENTER> 

This will start the PostgreSQL server using the cluster 'petra' from the command line. After the command executed successfully, this command window is then running the PostgreSQL server until the PostgreSQL server is stopped and the command window can therefore not accept any further commands.

  • In case you get an error message 'Execution of PostgresSQL by a user with administrative permissions is not permitted. The server must be started under an unprivileged user ID to prevent possible system security compromises. See the documentation for more information how to properly start the server': You need to run the command in a command window that is not associated with your current windows user, as this windows user has administrative privileges. To achieve that, open the command window using the 'Run...' menu item from the start menu and enter the following: runas /user:postgres cmd. You will be asked to enter the username for the postgres user. After entering that, execute the postgres -D "petra" command in this command window. The command should not give the error message anymore. (See this forum post for details.)
  • In case you get an error message 'FATAL: could not create lock file "postmaster.pid": Permission denied': ensure that the user that the PostgreSQL server is running under (usually 'postgres') has full access permissions to the folder that is above the 'data' directory, e.g. 'C:\Program Files\PostgreSQL\9.0'. You would check this and change this using Windows Explorer. (See this forum post for details.)

Instead of using the command line you can also use the program pgadmin3. Add there first a new server-connection (Name + Host = localhost, User: postgres, Password: the one that you entered during postgres-installation). Then in the left window-part go to "databases" and add a new one called "petra" with owner "postgres".

Creating a user

Open a new command window and again go to the Postgres program folder (e.g. C:\Program Files\PostgreSQL\9.0\bin\). Execute the following command there:

psql -c "CREATE USER mywindowsusername WITH PASSWORD 'anypassword';" <ENTER>
  • Please replace 'mywindowsusername' with the username that you log in to Windows when developing OpenPetra to avoid problems at a later stage when you will create the OpenPetra DB using the nant recreateDatabase command.
  • You can choose any password you want for that PostgreSQL user, it doesn't need to be your real windows password - and in fact it shouldn't, as you will need to put it in plaintext into the OpenPetra.build.config file at a later stage.

Instead of executing the CREATE USER command you could also use the program pgAdmin. When doing that, ensure that the new user has privileges to create a new database and for creating roles.


When you got to this point you can follow the instructions here to create the OpenPetra Database in the PostgreSQL cluster.


In case you forgot your postgres-Password do fe. the following:

- Uninstall PostgreSQL
- execute the following command on the windows command line: NET USER postgres /DELETE
- Reinstall PostgreSQL

Configuring OpenPetra

You must create a config file so that OpenPetra knows to use PostgreSQL. The file should be named OpenPetra.build.config and should be added to your root directory (note: OpenPetra.build is a different file that resides in the root directory). If OpenPetra.build.config already exists, then add your entries to the existing file.

The PostgreSQL config is a set of XML statements which specify the parameters to be used by OpenPetra. The file structure should be similar to the following example:

<?xml version="1.0"?>
<project name="OpenPetra-userconfig">
    <property name="PostgreSQL.Version" value="9.2" /> <!-- 9.0 is still the default -->
    <property name="PostgreSQL.exe" value="D:\Program Files\PostgreSQL\9.2\bin\psql.exe" />
    <property name="DBMS.Type" value="postgresql" />
    <property name="DBMS.DBName" value="myOpenPetraDb" />
    <property name="DBMS.UserName" value="myUserId" />
    <property name="DBMS.Password" value="myPassword" />
</project>

Notes:

  • PostgreSQL.Version - Required for Windows. Specifies the database version that is installed
  • PostgreSQL.exe - Optional. Defines the path to the PostgreSQL Interactive Terminal application. This entry is not required if PostgreSQL has been installed to the default location ("Program Files" or "Program Files(x86) for 32-bit and 64-bit Windows respectively).
  • DBMS.Type - Required. Identifies that the database type is PostgreSQL.
  • DBMS.DBName - Optional. Specifies the name of the database. Not required if standard paameters are used.
  • DBMS.UserName - Optional. Specifies the user name for the OpenPetra logon. Not required if standard paameters are used.
  • DBMS.Password - Optional. Specifies the user password for the OpenPetra logon. Not required if standard paameters are used.
  • See also User defined configuration parameters with OpenPetra.build.config


When Encountering an Error on OpenPetra Server Startup

If you get the following error message when starting the OpenPetra server: 'Exception occurred while establishing a connection to Database Server. ... System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.' and you are sure that the PostgreSQL DB Server's IP address and Port are correct and you are sure that the username and the password for connecting to the OpenPetra DB are right then it is most likely that the authorisation for your PC that is trying to access the PostgreSQL DB Server is missing from the PostgreSQL DB Server's pg_hba.conf file.

Solution:

  • Open the file /etc/postgresql/9.0/main/pg_hba.conf (on Debian Linux) or /var/lib/pgsql/data/pg_hba.conf (on CentOS and RedHat Linux) or C:\Program Files\PostgreSQL\9.0\data\pg_hba.conf (on Windows) in a text editor and add a line like the following at the end of the file:
host   openpetra  petraserver   xx.yyy.zz.xx/0   md5

- replacing xx.yyy.zz.xx with the IP Address of the PC that you are trying to start the OpenPetra Server on.

  • Restart the PostgreSQL DB Server from the command line (or if you use pgAdminIII: right-click on the PostgreSQL DB Server in the Object Browser and choose 'Reload configuration').
  • Restart the OpenPetra Server. The error should be gone. If it isn't, check that the PostgreSQL DB Server's IP address and Port are correct and you are sure that the username and the password for connecting to the OpenPetra DB are right.


Additional notes regarding pg_hba.conf:

  • at least in PostgreSQL 8.x, the order of the lines in that file is important. This means, I had to add the line for the petraserver user before the "host all" line.
  • for local access with psql, I need the row before local all all ident sameuser
local   openpetra petraserver   md5


  • if /etc/init.d/openpetra init gives this error:
psql: FATAL:  no pg_hba.conf entry for host "::1"
  • solution: add to the top of pg_hba.conf:
host all all ::1/128 md5

Some notes

  • drop database but tables are still there when creating the database again: [1]; reason: tables are in template1 db, and that is used as a template for creating the new database
  • drop all tables in db: DROP SCHEMA public CASCADE; [2]
    • you should create a new schema public again (especially if this is the template1 db): CREATE SCHEMA public;


  • Hide notice during nant recreateDatabase: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "..." for table "..."
  • If you get problems logging in to the database via nant recreateDatabase then please look here