Set initdb and pg_ctl - postgresql

I installed Postgresql from source.
I tried running the commands:
which initdb
which pg_ctl
but I get a blank response.
I know where these executables reside in my directory.
How might I set initdb and pg_ctl?
Thanks for your help.

You have received the blank output for command 'which', as these binaries are not known to your linux machine. And hence you will also need to run these binaries using './' (like ./pg_ctl)
You can add the path of your postgres bin directory to $PATH.
Eg.
export PATH=$PATH:/Postgres/Installation/Path/bin/
You can also set it permanently by adding the above path value in .bash_profile file in home of directory of Postgres user.

Related

Unable to use pg_ctl in Windows CMD?

pg_ctl status
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
C:\Users\Think>pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.
I don't know where is the pg_data directory, I set the C:\Users\Think\Desktop\postgres\data as User variable and System Variable In windows setting.
But still not working.
In the bin directory, there is pg_ctl.exe file.
You need to use the -D option to tell pg_ctl where your data directory is:
pg_ctl -D "Q:\path\to\datadir"
If you have created a data directory, you should be able to find it in cmd.exe with
dir C:\ /s /b | find PG_VERSION
if it is on C:.
If you just clicked through the installer, it might be in
C:\Program Files\PostgreSQL\12\data
use pg_ctl using the bin directory path of postgresql. Like root/.../../postgresql/bin/pg_ctl <then your command for restart, etc>

brew services start postgresql not working

I have installed postgres in Mac using command
brew install postgresql#9.6
and is successfully installed
brew services start postgresql#9.6 is successful also
==> Successfully started postgresql#9.6 (label: homebrew.mxcl.postgresql#9.6)
But when I tried to access Postgres from my Mac terminal psql postgresql#9.6 it prompt me weird error
-bash: /usr/local/bin/psql: No such file or directory
Am I doing something wrong, how to open psql from my terminal
Why there is no psql file in my /usr/local/bin
The accepted answer is incorrect.
If you install postgresql#9.6 via Homebrew, the right path for the /bin folder is:
/usr/local/Cellar/postgresql#9.6/9.6.15/bin
To connect, just type: psql postgres
If you're curious about the connections details, just type \conninfo and check it.
You must to enabling Postgres command line tools.
If you are using the default terminal, you are going to want to modify the file at
~/.bash_profile
. If you are using something like Oh My Zsh you are going to want to modify the file ~/.zshrc.
To edit this file you likely need to open it via the terminal, so open your terminal and type open ~/.bash_profile. You can replace the word open with subl or whatever text editor you prefer.
Once your zbash_profile or .zshrc file is open, add the following line to the end of the file:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
After that you will need to quit and restart your terminal This is to make sure it reloads with the changes you just made.
Once you have restarted your terminal, try running psql.
psql -U postgres
You should get the following output.
psql (9.6.0)
Type "help" for help.
postgres=#
You can read more on https://www.calhoun.io/how-to-install-postgresql-9-6-on-mac-os-x/

Error saving psql history file

My current psql version is 10.1,
but when I type \q to save the history it shows:
postgres=# \q could not save history to file "/opt/PostgreSQL/9.3/.psql_history": No such file or directory
But I am using Postgres 10.1, how to fix this?
How psql determines the path of the history file is documented as:
HISTFILE
The file name that will be used to store the history list. If unset, the file name is taken from the PSQL_HISTORY environment
variable. If that is not set either, the default is ~/.psql_history,
or %APPDATA%\postgresql\psql_history on Windows
You must be aware thatpsql does not use HOME to figure out the home directory represented by the tidle character, it uses /etc/passwd.
So presumably in the question psql is launched by the postgres user, and when this user was created, it was to install PostgreSQL 9.3 on this machine, and it was not changed afterwards when /opt/PostgreSQL/9.3/ got deleted, so the entry in /etc/passwd still points to that non-existing directory.
This answer on DBA.se give the shell command to fix that:
sudo usermod --home '/path/to/database' postgres

Register and run PostgreSQL 9.0 as Windows Service

For a while i have my db running on a command window because im not figuring out how to run it as a windows service.
Since i have the zip file version downloaded. how can i register the pg_ctl command as a windows service?
By the way, im using the following line to start the server:
"D:/Program Files/PostgreSQL/9.0.4/bin/pg_ctl.exe" -D "D:/Program Files/PostgreSQL/9.0.4/db_data" -l logfile start
Thanks in advance.
Use the register parameter for the pg_ctl program.
The data directory should not be stored in Program Files, the location of %ProgramData% is e.g. a good choice.
pg_ctl.exe register -N PostgreSQL -U some_windows_username -P windows_password -D "%ProgramData%/db_data" ...
In newer versions of Postgres, a separate Windows account is no longer necessary, so the following is also sufficient
pg_ctl.exe register -N PostgreSQL -D "%ProgramData%/db_data" ...
Details are in the manual: http://www.postgresql.org/docs/current/static/app-pg-ctl.html
You need to make sure the directory D:/Program Files/PostgreSQL/9.0.4/db_data has the correct privileges for the windows user you specify with the -U flag.
Btw: it is a bad idea to store program data in Program Files. You should move the data directory somewhere outside of Program Files because Program Files is usually highly restricted for regular users - with a very good reason.
Just run 'Command Prompt' as windows administrator and run the below command:
pg_ctl.exe register -N PostgreSQL -D "D:/Program Files/PostgreSQL/9.0.4/db_data"
You don't need to specify a User and Password, as previous answers have suggested.

postgres.app installed but which psql displays /usr/bin/psql and not path entered into zshrc

Having an issue with postgres.app where its installed, its running, but when I enter
which psql
I then receive "/usr/bin/psql" and not the correct path that postgres.app expects.
When I echo my PATH I get the following:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/Users/username/.rvm/bin:/usr/local/mysql/bin:/Applications/Postgres.app/Contents/MacOS/bin:/Users/username/.rvm/gems/ruby-1.9.3-p0/bin:/Users/username/.rvm/gems/ruby-1.9.3-p0#global/bin:/Users/username/.rvm/rubies/ruby-1.9.3-p0/bin:/Users/username/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Applications/MAMP/Library/bin/mysql:/Users/username/.rvm/bin
As you can see its in my Path but I have no idea why it wont choose the correct psql. Any assistance would be appreciated.
If psql is both in /usr/bin and /Applications/Postgres.app/Contents/MacOS/bin, the one in /usr/bin will be preferred according to your PATH specification. Consider putting the /Applications/... entry before /usr/bin:
/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin:...
You can also edit /etc/paths and make sure /Applications/Postgres.app/Contents/MacOS/bin is on the top. More info:
https://stackoverflow.com/a/6772559/2643002