4

Is there a way to find the PostgreSQL installation directory (bin) via query?

Thanks.

2
  • I'm afraid to ask what do you want to do. It sounds like a bad idea. Commented Sep 15, 2011 at 20:25
  • Ideas are never good or bad, intentions are. I need to know the path of bin directory via query, and then use it in the frontend. Since users can install postgresql in any directory they desire, I cannot hardcode it. :) Commented Sep 15, 2011 at 20:30

5 Answers 5

9

I don't think there is a way to get the path of the Postgres executable.

The only thing that can be retrieved from the system catalogs is the location of the data directory.

SELECT *
FROM pg_settings
WHERE name = 'data_directory'

This can only be executed by the superuser though.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I have already done that. select * from pg_settings order by length(setting) desc;
Is it safe to assume that bin directory will be stored relatively in all installation cases?
No, not at all. The bin directory will usually be in a completely different path - especially on *nix systems but probably on Windows because the "Program Files" directory is usually not writeable for regular users.
2

I would simply create a table and save the info you want in it as part of your installation procedure.

2 Comments

If you don't control the installation, why do you care? (You should not care)
I would suggest that you create the table just like all your other tables for your app, but also ship your customers a script they can run that will detect the bin path and update/insert the appropriate data, including creating the table if that's appropriate
0

On a Linux system, there will be a /etc/init.d/postgresql script file (possibly with a trailing version number) in which there is a PGENGINE configuration variable which specifies the bin directory, like: PGENGINE=/usr/bin. Other config variables there may also be of interest.

Comments

0

You can get the process identifier of the backend (=postgres process) using:

select pg_backend_pid();

How to determine the executable path for a process identifier depends on your OS. On Linux you can just do realpath /proc/12345/exe

Comments

0

Building on Jakob's answer, this is how I did it using psql:

psql -t -c '\o | xargs -I {} realpath /proc/{}/exe | xargs dirname' -c 'select pg_backend_pid()' -c 'select pg_sleep(1)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.