Manage Database from setup - Advanced Installer Community
cloudius
Posts: 3
Joined: Tue Aug 23, 2016 3:24 pm

Manage Database from setup

Hi,

I'm new at Advanced installer and so I hope to get here some clues to develop my first a little more complex setup.

The Situation:

I want to deploy a .NET Application (including a Windows-Service), using a PostgreSQL Database. This Application is for non technical educated users. They are dealing with a huge amount of Data, on a Stand alone System and dind't know anything about the problems of processind many datasets. They don't know anything about Databases and in most cases don't want to. So the Setup has to be as simple as possible, like the Program.

I builded a Offline-Setup including the -NET Framework, the PostgreSQL-Server and some ODBC Drivers. I install each of this Components as Prerequirement if they where not found at the customers system.

While Installing the PostgreSQL Database I set a default master password.

At the end of the Setup I connect to the Database-Server as superuser, create a database-user and a Database owned by the new user.

All this works fine on a naked System. All components become installed, the user and database are created successfully and the service starts work without any problems.

The Problems:
  1. In general the Client System hasn't a PostgreSQL Server installed but of cause I can't avoid the posibility that it has.
    So in that case the setup will detect the Server and won't install it by itself (thats fine) but it still has to create the user and Database. It will fail to connect to the Database, because the Masteruser Password is incorrect.

    So my idea is to show the user in this situation (and only in this situation) a Dialog asking for the superuser Password to proceed with the entered password. To do this I have to try to connect to the database server with the supposed password. If a error occurs (can't connect) I have to be notice this (without showing an errormessage to the user) and show the dialog.

    Is this possible within the Advanced Installer (Enterprice)?
  2. The second case is, that the current setup run is some kind of update. So my Masterpassword fits and my user and database are already existing at the database server.
    In this case I must recognice it to don't try to create my Database again (I can build some sort of CREATE USER IF NOT EXIST statement but not a CREATE DATABASE IF NOT EXISTS because PostgreSQL becomes a little eccentric in this situation).

    So my idea is to run some sort of "Check if exists query", sore the result an a Advanced-Variable and run the "CREATE DATABASE query" only if the result tells me the database won't exist on the server.

    And again I have to ask, is this possible within the Advanced Installer (Enterprice)?
regards,
cloudius
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Manage Database from setup

Hi Cloudius and welcome to our forums.

Thank you for your interest in Advanced Installer. Please accept my apologize for the delayed reply.
In general the Client System hasn't a PostgreSQL Server installed but of cause I can't avoid the posibility that it has.
So in that case the setup will detect the Server and won't install it by itself (thats fine) but it still has to create the user and Database. It will fail to connect to the Database, because the Masteruser Password is incorrect.

So my idea is to show the user in this situation (and only in this situation) a Dialog asking for the superuser Password to proceed with the entered password. To do this I have to try to connect to the database server with the supposed password. If a error occurs (can't connect) I have to be notice this (without showing an errormessage to the user) and show the dialog.
To achieve this you can, indeed, go in the "Search" page and create a search to see if PostgreSQL Server is installed on the machine. Then create a dialog where the user can introduce his password and use the "Show only if..." option to display that dialog only when the PostgreSQL Server is found on the machine.
The second case is, that the current setup run is some kind of update. So my Masterpassword fits and my user and database are already existing at the database server.
In this case I must recognice it to don't try to create my Database again (I can build some sort of CREATE USER IF NOT EXIST statement but not a CREATE DATABASE IF NOT EXISTS because PostgreSQL becomes a little eccentric in this situation).

So my idea is to run some sort of "Check if exists query", sore the result an a Advanced-Variable and run the "CREATE DATABASE query" only if the result tells me the database won't exist on the server.
Please keep in mind that in order to create a database you do not need to run a separate script. You can go in the "SQL Databases" page, add a new connection and check the "Create database if it does not exist" option. However, if you want to run some scripts on the first install, but not on upgrade, you can use the "NOT OLDPRODUCTS" condition.

Let us know if this helped, otherwise give us more details about your scenario.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cloudius
Posts: 3
Joined: Tue Aug 23, 2016 3:24 pm

Re: Manage Database from setup

Eusebiu wrote: To achieve this you can, indeed, go in the "Search" page and create a search to see if PostgreSQL Server is installed on the machine. Then create a dialog where the user can introduce his password and use the "Show only if..." option to display that dialog only when the PostgreSQL Server is found on the machine.
Thank you very much for the "Show only if..." clue, I haven't recognized this possibility until now.
The problem is, I do not only need to verify that the Database is installed (I can do this by searching Registry values) I also need to prove that a special database user (with known password) is (not) existing at the database. The only way to do this, as far as I can imagine, is to connect to the PostgreSQL with this user and check if the connection fails. But as far as I can see, I can't create a search doing this.
Eusebiu wrote:
Please keep in mind that in order to create a database you do not need to run a separate script. You can go in the "SQL Databases" page, add a new connection and check the "Create database if it does not exist" option. However, if you want to run some scripts on the first install, but not on upgrade, you can use the "NOT OLDPRODUCTS" condition.
Once again thank you for the Options clue and once again the Special User is the Problem. The "Application User" must be the owner of the "Application Database" so I have to create user first to create the Database owned by him. This should be also the case if I crate it by connecting to it (with the data of my own user).

so currently I run the Scripts:

Code: Select all

DO
$body$
BEGIN
   IF NOT EXISTS (
      SELECT *
      FROM   pg_catalog.pg_user
      WHERE  usename = 'myuser') THEN

      CREATE ROLE myuser LOGIN PASSWORD 'mypassword';
   END IF;
END
$body$;
and

Code: Select all

CREATE DATABASE "mydatabase"
  WITH OWNER = myuser
       ENCODING = 'UTF8'
       TABLESPACE = pg_default       
       CONNECTION LIMIT = -1;
So to make the setup bulletproof I have to verify 1. if the user exists and 2. if the database exists. But I don't find a proper way to do this.

regards
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Manage Database from setup

Hi,
Thank you very much for the "Show only if..." clue, I haven't recognized this possibility until now.
The problem is, I do not only need to verify that the Database is installed (I can do this by searching Registry values) I also need to prove that a special database user (with known password) is (not) existing at the database. The only way to do this, as far as I can imagine, is to connect to the PostgreSQL with this user and check if the connection fails. But as far as I can see, I can't create a search doing this.
In order to test if a specific username and password can connect to a database you can follow the steps described in the "How to test SQL connection parameters" tutorial. This method will set the "SqlConnectionOK" property to "1" if the connection was successful.
Once again thank you for the Options clue and once again the Special User is the Problem. The "Application User" must be the owner of the "Application Database" so I have to create user first to create the Database owned by him. This should be also the case if I crate it by connecting to it (with the data of my own user).
You can copy the above property in a public property through a "Set installer property" custom action and use it as a condition for your connection for the database creation.

Just let me know if there is anything I missed.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cloudius
Posts: 3
Joined: Tue Aug 23, 2016 3:24 pm

Re: Manage Database from setup

Great, this sounds promising.

I will try it soon.

Thank you very much!
Eusebiu
Posts: 4971
Joined: Wed Nov 14, 2012 2:04 pm

Re: Manage Database from setup

You're welcome. Glad to help.

Just let me know if you have any other question.

Best regards,
Eusebiu
Eusebiu Aria - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”