You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server 2012中PostgreSQL 10无法后台运行求助

Fix PostgreSQL Background Persistence on Windows Server 2012

Hey there, I get exactly what you're dealing with—running PostgreSQL via pg_ctl in cmd works until you close the window, which kills the server. That's because the PostgreSQL process is tied directly to the cmd session you launched it from. To get it running in the background like a proper system service (just like Linux's sudo service postgres start), here are the best solutions:

This is the standard, long-term fix. Services run independently of user sessions, so they'll keep running even if you log out or close all cmd windows. Plus, you can set it to auto-start on server boot.

  1. Open cmd as Administrator (right-click cmd.exe and select "Run as administrator"—you need elevated permissions to install a system service).
  2. Run this command to register the service:
    pg_ctl register -N "PostgreSQL 10" -D "\My postgres path\10\data"
    
    • -N "PostgreSQL 10" sets a friendly name for the service (feel free to tweak this, just be consistent with later commands).
    • -D points to your PostgreSQL data directory—keep the path wrapped in quotes if it includes spaces, like you already are doing.
  3. Now you can manage the service using Windows standard commands:
    • Start the service:
      net start "PostgreSQL 10"
      
    • Stop the service:
      net stop "PostgreSQL 10"
      
    • Set it to auto-start on server boot:
      Either open services.msc (type it in cmd or the start menu), find "PostgreSQL 10", right-click > Properties > Startup type > Auto.
      Or use this command:
      sc config "PostgreSQL 10" start= auto
      
      Note: There's a space after start=—that's required for the sc command to work correctly.

If you just need a quick temporary fix without installing a service, you can launch pg_ctl in the background using the start /B command:

start /B pg_ctl -D "\My postgres path\10\data" start

This will run the server without tying it directly to your cmd window, but warning: if you log out of your Windows session, the process will still terminate. This is only good for short-term testing, not for persistent production use.

Why Your Original Command Didn't Work

When you run pg_ctl start directly in cmd, PostgreSQL runs as a child process of the cmd window. Windows automatically terminates all child processes when the parent cmd window is closed. Services, on the other hand, run in the system's background session, so they're completely independent of user windows or sessions.


内容的提问来源于stack exchange,提问作者Frank Liao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:10:11