Unable to use pg_ctl in Windows CMD? - postgresql

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>

Related

Error in run sql file in postgres | No such file or directory

Problem
when in tried run sql file in psql shell...
give "No such file or directory" error!
$ ls
config.sql config.yaml
$ sudo -i -u postgres psql
postgres=# \i config.sql
config.sql: No such file or directory
thanks for your reply!
Quick solution:
-i => goes to user's home directory!
as result ./config.sql address is incorrect!
just use
$ psql -U <user_name>
postgres=# \i config.sql
man sudo tells you:
-i, --login
Run the shell specified by the target user's password database entry as a login shell. This means that login-specific
resource files such as .profile, .bash_profile or .login will be read by the shell. If a command is specified, it is passed
to the shell for execution via the shell's -c option.
In particular, that will set your current working directory to the home directory of user postgres.
If you want to avoid that, don't use '-i'.

Permanently set PGDATA environment variable Mac

To start my Postgres server I need to run these commands:
pg_ctl -D /usr/local/var/postgres start
export PGDATA='/usr/local/var/postgres'
Isn't there a way to define the PGDATA permanently and from there on only use commands like pg_ctl start to operate in the shell? Thanks.
You're half way there. The only thing you might want to do is to get PGDATA whenever you open the terminal by adding the export line to your shell config file. On my macOS machine i added the following line:
export PGDATA='/Users/john/.postgres' to ~/.bash_profile file. Don't forget to source it if you want to see the effect immediately by executing . ~/.bash_profile command. That's it from now on you can start your posters server by simply typing pg_ctl start. Hope it was worth the wait for the answer. :)
export will set the context for your current session. So we need to hack our terminal so that we will export this PGDATA env variable to every session.
If you are using zsh, you can simply execute the below command.
echo "export PGDATA='/usr/local/var/postgres'" >> ~/.zshrc
In case you are not using zsh replace ~/.zshrc in the above command with ~/.bashrc or ~/.profile
then, do source ~/.zshrc to see the immediate effect. You can instantly check using echo $PGDATA command.
Whenever you open a new terminal, your environment variable will be available by default.

Set initdb and pg_ctl

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.

Postgres cannot see my PGDATA environment variable

Can anyone explain this:
~$ echo $PGDATA
/Library/PostgreSQL/9.2/data
~$ cd /Library/PostgreSQL/9.2/
/Library/PostgreSQL/9.2$ sudo su postgres
bash-3.2$ echo $PGDATA
<blank line>
bash-3.2$ pg_ctl start
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
bash-3.2$ export PGDATA="/Library/PostgreSQL/9.2/data"
bash-3.2$ pg_ctl start
server starting
bash-3.2$
The following is in my ~/.bashrc file:
export PGDATA="/Library/PostgreSQL/9.2/data"
I'm not mis-spelling the path in PGDATA:
~$ echo $PGDATA
/Library/PostgreSQL/9.2/data
export PGDATA="/Library/PostgreSQL/9.2/data"
sudo can't be assumed to keep the environment variables of its caller.
See its manpage for the details with your specific OS.
As far as I know, environment variables are attached to a shell, and as far as I can tell I'm in the same shell.
This works for me:
~$ cd /Library/PostgreSQL/9.2/
/Library/PostgreSQL/9.2$ sudo -E su postgres
...
-E The -E (preserve environment) option will override the
env_reset option in sudoers(5)). It is only available when
either the matching command has the SETENV tag or the
setenv option is set in sudoers(5).
I only understand the following:
The -E (preserve environment) option
Next comment:
When I su to postgres I do su - postgres. Notice the hyphen.
Here is what my man pages say about the hyphen:
-l Simulate a full login. The environment is discarded except for
HOME, SHELL, PATH, TERM, and USER. HOME and SHELL are modified
as above. USER is set to the target login. PATH is set to
``/bin:/usr/bin''. TERM is imported from your current environ-
ment. The invoked shell is the target login's, and su will
change directory to the target login's home directory.
- (no letter) The same as -l.
I don't see how that will preserve the PGDATA environment variable, and this is what happened when I tried the hyphen:
/Library/PostgreSQL/9.2$ sudo su - postgres
Password:
7studs-computer:~ postgres$ ls
bin pgAdmin3.app
data pg_env.sh
doc scripts
include share
installer stackbuilder.app
lib uninstall-postgresql.app
7studs-computer:~ postgres$ pg_ctl start
-bash: pg_ctl: command not found
7studs-computer:~ postgres$ ls
bin pgAdmin3.app
data pg_env.sh
doc scripts
include share
installer stackbuilder.app
lib uninstall-postgresql.app
7studs-computer:~ postgres$ cd bin
7studs-computer:bin postgres$ ls
clusterdb pg_config pgbench
createdb pg_controldata pltcl_delmod
createlang pg_ctl pltcl_listmod
createuser pg_dump pltcl_loadmod
dropdb pg_dumpall postgres
droplang pg_receivexlog postmaster
dropuser pg_resetxlog psql
ecpg pg_restore reindexdb
initdb pg_standby vacuumdb
oid2name pg_test_fsync vacuumlo
pg_archivecleanup pg_test_timing
pg_basebackup pg_upgrade
7studs-computer:bin postgres$ ./pg_ctl start
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.
7studs-computer:bin postgres$
So the hyphen doesn't work for me (OSX 10.6.8).

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.