postgresql Postgres 看不到我的 PGDATA 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26045859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:37:41  来源:igfitidea点击:

Postgres cannot see my PGDATA environment variable

postgresqlenvironment-variables

提问by 7stud

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:

以下是我的 ~/.bashrc 文件:

export PGDATA="/Library/PostgreSQL/9.2/data"

I'm not mis-spelling the path in PGDATA:

我没有拼错 PGDATA 中的路径:

                ~$ echo $PGDATA
                /Library/PostgreSQL/9.2/data
 export PGDATA="/Library/PostgreSQL/9.2/data"

采纳答案by 7stud

sudo can't be assumed to keep the environment variables of its caller. See its manpage for the details with your specific OS.

不能假定 sudo 保留其调用者的环境变量。有关您的特定操作系统的详细信息,请参阅其联机帮助页。

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.

据我所知,环境变量附加到一个 shell,据我所知,我在同一个 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.

当我 su 到 postgres 我做 su - postgres。注意连字符。

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:

我看不出这将如何保留 PGDATA 环境变量,这就是我尝试使用连字符时发生的情况:

/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).

所以连字符对我不起作用(OSX 10.6.8)。