Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

postgresql - How to install postgres with NSIS with all parameters?

We have a C# program which uses postgres as its database, so we want to bundle the postgres in the installer as a semi-silent install, i.e. the user will be notified about the mandatory installation, but the installation itself will be automatically performed without the need for user input.

After researching opinions on WiX, Inno Setup and NSIS we decided to use NSIS. As I see it the easiest way will be to include the binary .zip distibution of postgres and perform the following in the script:

  1. Copy the pgsql folder to the installation dir
  2. Run initdb.exe with the appropriate arguments to create the database itself in InstallDir/pgsql/data
  3. Run pg_ctl.exe to register as a service with application specific name
  4. Use the NSIS Simple Service Plugin to start the service
  5. Install our program

I have several questions about this scenario:

Q1: Am I on the right track at all?

Q2: Can I pass the superuser password somehow to initdb.exe, so it doesn't ask the user to enter it trough the console?

Q3: Where and how do I change the port on which postgres will listen, so that I don't conflict with other installed instances?

Q4: When uninstalling is it enough to just unregister the service with pg_ctl.exe and delete all folders?

PS: I feel the questions are connected to each other so I've put them all together here. But if needed I can separate them in different questions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is about right.

Please do not use port 5432 for PostgreSQL. Pick a non-default port well outside that range, like 5599 or something. Change the port by modifying postgresql.conf and setting the port directive. You might find it useful to simply append include_dir = conf.d or similar in your main postgresql.conf then create a datadirconf.dmyapp.conf with your configuration overrides.

To set the password for initdb without a prompt, pass the --pwfile=/path/to/file option. The first line will be read as the password. See initdb.

Your uninstall plan is reasonable and correct.

Think about upgrades though - don't paint yourself into a corner. You'll want to be able to install 9.5 in parallel to 9.4 and pg_upgrade, unless you want to just use dump and restore.

I suggest installing the PostgreSQL binaries into %PROGRAMFILES%MyAppPostgreSQL9.4. You should probably put the database into %PROGRAMDATA%MyAppPostgreSQL9.4 instead of your app's %PROGRAMFILES% directory. (The PostgreSQL installer should do the same; its current behaviour is a historical artifact that should be fixed).

Please document that your application bundles PostgreSQL so that nobody "cleans" it up, and so we don't have yet another app spewing confused users onto pgsql-general. It gets tiring explaining to angry users that "we" didn't install PostgreSQL on their system, we can't remove it, it's probably there because it was installed by something they use, and if they delete it then that'll stop working. Similarly, it gets tiring explaining to users who just killed the PostgreSQL processes and deleted their PostgreSQL data dir that we can't get their Poker Tracker (or whatever) database back because they just deleted it...

Make sure your app exposes functionality to do PostgreSQL dumps and restores for users.

The user manual must clearly explain that they need to take extra backup steps to protect data in your app. You can't just use Windows Backup to get a reliable PostgreSQL backup because it's multiple files that must be copied together; it'll copy each file OK, but the combined result will be unusable unless PostgreSQL was stopped before the backup. To do a live backup you need to take special steps - pg_start_backup(), copy, pg_stop_backup() and archive the extra WAL segments, or use pg_basebackup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...