无法将 PostgreSQL 作为 Windows 服务运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1251233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:19:29  来源:igfitidea点击:

Unable to run PostgreSQL as Windows service

postgresqlservice

提问by David

I had this in my Windows services:

我的 Windows 服务中有这个:

C:/Program Files/PostgreSQL/8.4/bin/pg_ctl.exe runservice -N "postgresql-8.4" -D "D:/PostgreSQL/8.4/data" -w

It never finishes executing. But if I did this on the dos shell:

它永远不会完成执行。但是如果我在 dos shell 上这样做:

C:/Program Files/PostgreSQL/8.4/bin/pg_ctl.exe start -N "postgresql-8.4" -D "D:/PostgreSQL/8.4/data" -w

Notice that I only changed the "runservice" to "start" and it works just fine.

请注意,我只将“runservice”更改为“start”,它工作得很好。

Any idea?

任何的想法?

回答by Jay Rizzi

The command runservice can only be executed by the service manager

命令 runservice 只能由服务管理器执行

in order to fix my localhost windows 7 to start postgres as a service i used the following command to start the data

为了修复我的 localhost windows 7 以将 postgres 作为服务启动,我使用以下命令启动数据

 pg_ctl -D "C:\Program Files\PostgreSQL.1\data" start 

Then checked the status for errors

然后检查状态是否有错误

 pg_ctl -D "C:\Program Files\PostgreSQL.1\data" status 

if you get error 1063 , its more than likely permissions, i executed the following command

如果你得到错误 1063 ,它很可能是权限,我执行了以下命令

 cacls "C:\Program Files\PostgreSQL.1\data" /E /T /C /G postgres:F

then reran the start/status, it showed everything fine, but still service manager would not start the service

然后重新运行启动/状态,它显示一切正常,但服务管理器仍然不会启动服务

So, in Services->postgresql->options->logon i set the log on as the Local system account instead of the postgres user, and voila it worked

因此,在 Services->postgresql->options->logon 中,我将登录设置为本地系统帐户而不是 postgres 用户,瞧它起作用了

回答by pstanton

this happened to me because i set my data directory to be somewhere the postgres windows user account didn't have access to.

这发生在我身上,因为我将我的数据目录设置为 postgres Windows 用户帐户无法访问的地方。

回答by Stephan

I faced the same issue after moving manually the database data files(PG_DATA directory) without recreating all the necessary permissions.

手动移动数据库数据文件(PG_DATA 目录)后,我遇到了同样的问题,而没有重新创建所有必要的权限。

Here is how I solved my issue:

这是我如何解决我的问题:

1. Check permissions on old PG_DATA directory:

1. 检查旧 PG_DATA 目录的权限:

cacls "c:\path\to\old\pgdata\dir"

2. Check permissions on new PG_DATA directory:

2. 检查新 PG_DATA 目录的权限:

cacls "d:\path\to\NEW\pgdata\dir"

3. Compare outputs from 1. and 2.

3. 比较 1. 和 2 的输出。

Find the differences between users and/or permissions then synchronize them.

找出用户和/或权限之间的差异,然后同步它们。

Nota:I found it easier to use explorerfor the synchronization step rather than using caclsdirectly from the command line.

注意:我发现explorer在同步步骤中使用比cacls直接从命令行使用更容易。

回答by Tom

I had this problem in Windows after a system crash. Running the first command showed invalid data in C:\Program Files\PostgreSQL\9.1\data\postmaster.pid. Deleting that file did the trick. Reference.

系统崩溃后,我在 Windows 中遇到了这个问题。运行第一个命令在C:\Program Files\PostgreSQL\9.1\data\postmaster.pid. 删除那个文件就成功了。参考。

回答by onur

If you changed pg_hba.conf, maybe you missed somewhere in file. For example there must be CIDR after IP in that file. It must be like 192.168.1.100/32

如果您更改了pg_hba.conf,可能您错过了文件中的某个地方。例如,该文件中 IP 后必须有 CIDR。它必须像 192.168.1.100/32

If you forgot to put 32, then server doesnt restart.

如果您忘记输入 32,则服务器不会重新启动。

Investigation of startup logs could be a clue. For the case problem is in the pg_hba.confyou could see something like this:

调查启动日志可能是一个线索。对于案例问题在pg_hba.conf 中,您可以看到如下内容:

2018-11-13 00:39:34.841 PST [8284] FATAL:  could not load pg_hba.conf
2018-11-13 00:39:34.842 PST [8284] LOG:  database system is shut down

回答by Magnus Hagander

You need to check your logfiles and the windows eventlog for some hint of what the problem is. If there is nothing at all there, you need to break out something like Process Monitor and get a stacktrace of where it's hung.

您需要检查您的日志文件和 Windows 事件日志以了解问题所在。如果那里什么都没有,您需要打破进程监视器之类的东西,并获得它挂在哪里的堆栈跟踪。

回答by Grant Johnson

I have had this issue in the past, and it was that the installer did not set up the permissions correctly for the user that the service was to run as.

我过去遇到过这个问题,是安装程序没有为服务运行的用户正确设置权限。

回答by Tom Syscore Data Solutions

I've also ran into this problem with postgresql throwing and error after trying to initialize the database cluster. After analyzing the log files and running command line scripts for 4 hours I've got the solution to anyone running into this problem for Windows Versions.

在尝试初始化数据库集群后,我也遇到了 postgresql 抛出和错误的问题。在分析日志文件并运行命令行脚本 4 小时后,我已经找到了解决 Windows 版本中遇到此问题的任何人的方法。

This is not a detailed description as to why its happening. I've installed odoo 10, 11, 12 and 13 numerous times on countless client servers and windows systems and this is the first time I've ever ran into this problem. I cant say if its because I have MS VS Enterprise installed and Android Studio on this machine or what. But Below is the easy answer on how to fix it and initialize the cluster and create the database files in the data folder.

这不是关于其发生原因的详细描述。我已经在无数客户端服务器和 Windows 系统上多次安装了 odoo 10、11、12 和 13,这是我第一次遇到这个问题。我不能说是因为我在这台机器上安装了 MS VS Enterprise 和 Android Studio 还是什么。但下面是关于如何修复它并初始化集群并在数据文件夹中创建数据库文件的简单答案。

  1. Open the data folder for postgresql. - For Odoo installs it will normally be "C:\Program Files (x86)\Odoo 13.0\PostgreSQL" Unless you chose another location when installing.

  2. Remove any or all files from this folder - If not you will get an error when running initdb.exe

  3. Right click the data folder and open up the properties for it. Click on the Security tab and then click the advanced button on the bottom.

  4. You need to change the owner of this folder to openpgsvc. Click Change and type in openpgsvc and click ok. Once done click the check box below saying that you want this change to affect containers with this container as well.

  5. Then on the Permissions tab click the add button on the bottom. You need to add openpgsvc as a user and give this user full rights. Click apply and and ok to close out of all the folder properties.

  6. Now you need to open cmd.exe - Once open we are going to call initdb.exe and pass some values to it as well.

  7. First run chdir and change the working directory to the location of initdb.exe. For me, running odoo 13 on a windows 10 machine the location is this..

  1. 打开 postgresql 的数据文件夹。- 对于 Odoo 安装,它通常是“C:\Program Files (x86)\Odoo 13.0\PostgreSQL”,除非您在安装时选择了其他位置。

  2. 从此文件夹中删除任何或所有文件 - 如果没有,您将在运行 initdb.exe 时收到错误消息

  3. 右键单击数据文件夹并打开它的属性。单击安全选项卡,然后单击底部的高级按钮。

  4. 您需要将此文件夹的所有者更改为 openpgsvc。单击更改并键入 openpgsvc,然后单击确定。完成后,单击下面的复选框,表示您希望此更改也影响带有此容器的容器。

  5. 然后在权限选项卡上单击底部的添加按钮。您需要将 openpgsvc 添加为用户并授予此用户完全权限。单击应用和确定以关闭所有文件夹属性。

  6. 现在您需要打开 cmd.exe - 一旦打开,我们将调用 initdb.exe 并将一些值传递给它。

  7. 首先运行 chdir 并将工作目录更改为 initdb.exe 所在的位置。对我来说,在 Windows 10 机器上运行 odoo 13 的位置是这个..

"C:\Program Files (x86)\Odoo 13.0\PostgreSQL\bin"

"C:\Program Files (x86)\Odoo 13.0\PostgreSQL\bin"

  1. There is one variable that need to be passed as well to make this work here is the list. NEEDS TO BE INCLUDED IN THE CALL TO initdb.exe
  1. 还需要传递一个变量才能使这项工作在此处工作,即列表。需要包含在对 initdb.exe 的调用中

Postgres Data Dir: "C:\Program Files (x86)\Odoo 13.0\PostgreSQL\data"

Postgres 数据目录:“C:\Program Files (x86)\Odoo 13.0\PostgreSQL\data”

The End Result with the parameter would look like this for my installation:

对于我的安装,带有参数的最终结果如下所示:

"C:\Program Files (x86)\Odoo 13.0\PostgreSQL\bin\initdb.exe" -D "C:\Program Files (x86)\Odoo 13.0\PostgreSQL\data"

"C:\Program Files (x86)\Odoo 13.0\PostgreSQL\bin\initdb.exe" -D "C:\Program Files (x86)\Odoo 13.0\PostgreSQL\data"

  1. Hit Enter and let it rip. The output of this command should look like this below. Cmd.exe running initdb.exe script
  1. 按 Enter 并让它撕裂。此命令的输出应如下所示。 Cmd.exe 运行 initdb.exe 脚本

回答by Nxt_N_C

sc create "postgresql-9.2" binPath= "\"C:/Program Files (x86)/PostgreSQL/9.2/bin/pg_ctl.exe\" runservice -N \"postgresql-9.2\" -D \"C:/Program Files (x86)/PostgreSQL/9.2/data\" -w" DisplayName= "postgresql-9.2" start= auto

Try this on CMD run as Administrator (Add your parameters depend on your version)

在以管理员身份运行的 CMD 上试试这个(添加你的参数取决于你的版本)

回答by mins

Installing PostgreSQL 10 On Windows 7 (yes the clock is ticking...). I first tried the latest version 11 which completely failed to install... not a good sign for Windows users. Anyway.

在 Windows 7 上安装 PostgreSQL 10(是的,时钟在滴答作响......)。我首先尝试了完全无法安装的最新版本 11...对于 Windows 用户来说不是一个好兆头。反正。

Quick answer: Change the account in the Windows Services panel from Network to Local.

快速回答:将 Windows 服务面板中的帐户从网络更改为本地。

Details of my case

我的案件详情

During installation I created/selected a data folder in the user profile folder, because obviously the folder suggested by default, within the program folder, wouldn't work, and if it worked it would be a very idea to put data here (I don't know whether it's usual to do that on Unix/Linux, but for Windows it's it's not allowed for a long time).

在安装过程中,我在用户配置文件文件夹中创建/选择了一个数据文件夹,因为显然默认情况下建议的文件夹在程序文件夹中不起作用,如果它起作用,将数据放在这里是一个很好的主意(我不不知道在 Unix/Linux 上是否通常这样做,但对于 Windows,很长一段时间都不允许这样做)。

At the end (when populating the data cluster) I received an error:

最后(填充数据集群时)我收到一个错误:

Failed to load SQL Modules into database Cluster

无法将 SQL 模块加载到数据库集群中

but the installation was able to complete. I found two pages about previous error, hereand here, but they didn't seem relevant to my case, so I just started pgAdmin and, on the left "browser", saw the server wasn't active.

但安装能够完成。我在此处此处找到了两页有关先前错误的页面,但它们似乎与我的情况无关,因此我刚刚启动了 pgAdmin,并且在左侧的“浏览器”中看到服务器未处于活动状态。

I tried to start it from here (had to type the main password), but it went inactive immediately again. So I tried to use the Windows services panel to start "postgresql-x64-10", no joy. I copied the command from this panel and pasted it into a Windows console (cmd.exe) where I finally received this

我试图从这里启动它(必须输入主密码),但它立即再次处于非活动状态。所以我尝试使用 Windows 服务面板启动“postgresql-x64-10”,没有任何乐趣。我从这个面板复制了命令并将其粘贴到 Windows 控制台 (cmd.exe) 中,我终于在那里收到了这个

error 1063.

错误 1063。

Searching I found this related question, and was convinced the problem was about permissions.

搜索我发现了这个相关的问题,并确信问题出在权限上。

Solution working for my case

适用于我的案例的解决方案

In the services panel I changed the account used to start the service from Network Service to Local System as suggested in a comment by @AlexanderRios.

在服务面板中,我按照@AlexanderRios 的评论中的建议将用于启动服务的帐户从网络服务更改为本地系统。