在同一服务器部署两个PostgreSQL 10实例时遇initdb/pg_ctl命令异常求助
Hey there, let's work through this PostgreSQL issue together! It looks like your PostgreSQL 10 binary tools (like pg_ctl and initdb) aren't in your system's PATH, which is why whereis only finds the man pages. Plus, I'll walk you through setting up those two separate instances once we fix the tool access problem.
Step 1: Locate PostgreSQL 10's Binary Files
First, we need to find where the actual executable files are hiding. Depending on how you installed PostgreSQL 10, they're likely in one of two places:
- If you compiled from source:
/usr/local/pgsql/bin/ - If you used a package manager (like apt/yum):
/usr/lib/postgresql/10/bin/
Run this command to search your system for the pg_ctl binary:
find / -name pg_ctl -type f 2>/dev/null
The 2>/dev/null part just filters out annoying permission-denied errors.
Once you find the path, test if the tools work by running:
# Replace with your actual path, e.g., /usr/local/pgsql/bin/pg_ctl /path/to/pg_ctl --version /path/to/initdb --version
If you see the PostgreSQL 10 version number, the binaries are good—we just need to add them to your PATH.
Step 2: Add Binaries to PATH (Permanent Fix)
Switch to the postgres user and edit their bash profile to make the PATH change stick:
su - postgres nano ~/.bashrc
Add this line at the end of the file (replace the path with the one you found):
export PATH=/usr/local/pgsql/bin:$PATH
Save and exit, then reload the profile to apply the change:
source ~/.bashrc
Now run whereis pg_ctl again—you should see the binary file path listed alongside the man page.
Step 3: Set Up Two Independent PostgreSQL 10 Instances
With the tools accessible, let's create your two separate instances:
- Create data directories (make sure the
postgresuser owns them):
mkdir -p /var/lib/postgresql/10/instance1 mkdir -p /var/lib/postgresql/10/instance2
- Initialize each instance:
# Instance 1 (uses default port 5432) initdb -D /var/lib/postgresql/10/instance1 -U postgres -W # Instance 2 (uses port 5433) initdb -D /var/lib/postgresql/10/instance2 -U postgres -W
The -W flag forces you to set a password for the postgres user—make sure to note these down.
- Configure each instance's settings:
- For Instance 1, edit
/var/lib/postgresql/10/instance1/postgresql.conf:
Adjust the port (keep 5432 or change it) and allow remote connections if needed:port = 5432 listen_addresses = '*' # Remove this if you don't want remote access - For Instance 2, edit
/var/lib/postgresql/10/instance2/postgresql.conf:port = 5433 listen_addresses = '*' # Adjust as needed
- Start both instances:
# Start Instance 1 pg_ctl -D /var/lib/postgresql/10/instance1 start # Start Instance 2 pg_ctl -D /var/lib/postgresql/10/instance2 start
Verify they're running with:
psql -p 5432 -U postgres -c "SELECT version();" psql -p 5433 -U postgres -c "SELECT version();"
- Set up auto-start on boot (optional):
Create systemd service files for each instance to ensure they start when the server boots.
For Instance 1, create /etc/systemd/system/postgresql@10-instance1.service:
[Unit] Description=PostgreSQL 10 Instance 1 After=network.target [Service] Type=forking User=postgres Group=postgres ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /var/lib/postgresql/10/instance1 ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /var/lib/postgresql/10/instance1 -m fast ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D /var/lib/postgresql/10/instance1 Restart=always [Install] WantedBy=multi-user.target
Copy this file for Instance 2, rename it to postgresql@10-instance2.service, and update the paths/descriptions to match Instance 2.
Then enable and start the services:
systemctl daemon-reload systemctl enable postgresql@10-instance1 systemctl enable postgresql@10-instance2
Troubleshooting: If You Can't Find the Binaries
If the find command doesn't locate pg_ctl, your PostgreSQL 10 installation probably failed. Try reinstalling:
- For source installs: Re-run
make installwith sudo/root to ensure permissions are correct. - For package manager installs (Debian/Ubuntu): Run
apt install postgresql-10—this will place binaries in/usr/lib/postgresql/10/bin/automatically.
内容的提问来源于stack exchange,提问作者Khave

