将 postgres 超级用户授予 linux 帐户

tv_*_*gar 7 postgresql linux

在我的开发 (Ubuntu linux) 笔记本电脑上,我有一个postgres操作系统帐户,该帐户拥有 Postgres 安装。

当我想执行任何 postgres 活动、创建/删除数据库等时,我必须首先 su 到 postgres 帐户。

$ sudo su postgres
Run Code Online (Sandbox Code Playgroud)

如何更改我自己的操作系统帐户以获得postgres的操作系统级权限,这样我就不需要su 了

Cra*_*ein 5

在操作系统上创建用户

# Identify yourself as root
su - 

# Create the user who will have access to a postgres database
useradd mypostgresuser

# Add a password
passwd mypostgresuser
Run Code Online (Sandbox Code Playgroud)

授予本地用户访问 postgres 的权限

您需要找到 postgresql 安装的数据目录,即您创建数据库文件的位置。它们通常位于 /var/lib/pgsql/data 您的安装值可能在环境变量 $PGDATA 中可用

# Make sure that local users can access postgres
cat /${PGDATA}/pg_hba.conf

# this was the default setting on my 8.4 install
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all
Run Code Online (Sandbox Code Playgroud)

如果您进行任何更改,则需要重新加载 postgres

/etc/init.d/postgresql reload
Run Code Online (Sandbox Code Playgroud)

或者作为 postgres

pg_ctl reload -D ${PGDATA}

现在作为 postgres 连接到 psql

# Create the user in postgres
postgres=# create user mypostgresuser;
CREATE ROLE

# Give that user access to a database
postgres=# grant all privileges on database mytestdb to mypostgresuser;
GRANT
Run Code Online (Sandbox Code Playgroud)

测试连接

# Identify yourself as mypostgresuser
su - mypostgresuser

# Connect to the database
psql -d mytestdb 
Run Code Online (Sandbox Code Playgroud)