PostgreSQL 服务器的“root”执行是不允许的

问题描述 投票:0回答:5

当我尝试启动 postgresql 时出现错误:

postgres

postgres不知道去哪里找服务器配置文件
您必须指定 --config-file 或 -D 调用选项或设置 PGDATA 环境变量。

然后我尝试设置我的配置文件:

postgres -D /usr/local/var/postgres

我收到以下错误:

postgres 无法访问服务器配置文件“/usr/local/var/postgres/postgresql.conf”:权限被拒绝

嗯好吧。接下来,我尝试以管理员身份执行相同的操作:

 sudo postgres -D /usr/local/var/postgres

我收到以下错误:

PostgreSQL 服务器的“root”执行是不允许的。
服务器必须在非特权用户 ID 下启动,以防止 可能的系统安全妥协。有关更多信息,请参阅文档 有关如何正确启动服务器的信息。

我在谷歌上搜索了该错误消息,但找不到解决方案。
谁能对此提供一些见解?

postgresql sudo privileges
5个回答
68
投票

对于那些尝试使用官方 docker 镜像运行自定义命令的人,请使用以下命令。

docker-entrypoint.sh
处理切换用户和处理其他权限。

docker-entrypoint.sh -c 'shared_buffers=256MB' -c 'max_connections=200'

39
投票

你的命令并没有按照你的想法去做。以系统用户

postgres
运行某些东西:

 sudo -u postgres command

运行命令(也称为

postgres
!):

sudo -u postgres postgres -D /usr/local/var/postgres

你的命令做相反的事情:

sudo postgres -D /usr/local/var/postgres

它以超级用户

postgres
root
没有
sudo
开关)运行程序
-u
,出于安全原因,Postgres不允许以超级用户权限运行。因此错误消息。

如果您要以系统用户

postgres
运行几个命令,请将用户更改为:

sudo -u postgres -i

... 和

exit
当你完成时。

如果您在以系统用户身份运行时看到此错误消息

postgres
,则文件或其中一个包含目录的权限有问题。

postgres 无法访问服务器配置文件“/usr/local/var/postgres/postgresql.conf”: Permission denied /usr/local/var/postgres/postgresql.conf

考虑 Postgres 手册中的说明.
还要考虑包装器

pg_ctl
- 或基于 Debian 的发行版中的
pg_ctlcluster

并且知道
su
sudo
之间的区别。相关:


7
投票

Muthukumar 的回答是最好的!!经过一整天的搜索,通过更简单的方法更改我在 Kubernetes 中的 Alpine Postgres 部署,我找到了这个简单的答案。

有我完整的描述。享受它!!

首先我需要创建/定义一个具有正确值的 ConfigMap。保存在文件“custom-postgresql.conf”中:

# DB Version: 12
# OS Type: linux
# DB Type: oltp
# Total Memory (RAM): 16 GB
# CPUs num: 4
# Connections num: 9999
# Data Storage: ssd
# https://pgtune.leopard.in.ua/#/
# 2020-10-29
listen_addresses = '*'
max_connections = 9999
shared_buffers = 4GB
effective_cache_size = 12GB
maintenance_work_mem = 1GB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 209kB
min_wal_size = 2GB
max_wal_size = 8GB
max_worker_processes = 4
max_parallel_workers_per_gather = 2
max_parallel_workers = 4
max_parallel_maintenance_workers = 2

创建配置/地图:

kubectl create configmap custom-postgresql-conf --from-file=custom-postgresql.conf

请注意定义自定义设置中的值 根据Pod资源,主要是内存和CPU分配。

有清单(postgres.yml):

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
  namespace: default
spec:
  accessModes: [ "ReadWriteOnce" ]
  resources:
    requests:
      storage: 128Gi
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  namespace: default
spec:
  type: ClusterIP
  selector:
    app: postgres
    tier: core
  ports:
    - name: port-5432-tcp
      port: 5432

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
      tier: core
  template:
    metadata:
      labels:
        app: postgres
        tier: core
    spec:
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc
        - name: postgresql-conf
          configMap:
            name: postgresql-conf
            items:
              - key: custom-postgresql.conf
                path: postgresql.conf
      containers:
        - name: postgres
          image: postgres:12-alpine
          resources:
            requests:
              memory: 128Mi
              cpu: 600m
            limits:
              memory: 16Gi
              cpu: 1500m
          readinessProbe:
            exec:
              command:
                - "psql"
                - "-w"
                - "-U"
                - "postgres"
                - "-d"
                - "postgres"
                - "-c"
                - "SELECT 1"
            initialDelaySeconds: 15
            timeoutSeconds: 2
          livenessProbe:
            exec:
              command:
                - "psql"
                - "-w"
                - "postgres"
                - "-U"
                - "postgres"
                - "-d"
                - "postgres"
                - "-c"
                - "SELECT 1"
            initialDelaySeconds: 45
            timeoutSeconds: 2
          imagePullPolicy: IfNotPresent
          # this was the problem !!!
          # I found the solution here: https://stackoverflow.com/questions/28311825/root-execution-of-the-postgresql-server-is-not-permitted
          command: [ "docker-entrypoint.sh", "-c", "config_file=/etc/postgresql/postgresql.conf" ]
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgresql
            - name: postgresql-conf
              mountPath: /etc/postgresql/postgresql.conf
              subPath: postgresql.conf
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: etldatasore-username
                  key: ETLDATASTORE__USERNAME
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: etldatasore-database
                  key: ETLDATASTORE__DATABASE
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: etldatasore-password
                  key: ETLDATASTORE__PASSWORD

您可以申请

kubectl apply -f postgres.yml

转到您的 pod 并检查应用的设置:

kubectl get pods
kubectl exec -it postgres-548f997646-6vzv2 bash

bash-5.0# su - postgres
postgres-548f997646-6vzv2:~$ psql

postgres=# show config_file;
           config_file
---------------------------------
 /etc/postgresql/postgresql.conf
(1 row)

postgres=#

# if you want to check all custom settings, do
postgres=# SHOW ALL;

谢谢你Muthukumar!!

请自己尝试、验证并分享!!!


0
投票

终端返回 postgres command not found.

无法更新 postgresql


0
投票

我在两个方面遇到了这个问题:docker-compose 和 kubernetes 部署。

要在 docker-compose 上解决它,编辑命令 arg 就足够了:

services:
  db:
    image: postgres
    ports: 
      - 5432:5432
    command: postgres -c max_connections=200

对于 kubernetes 部署,我必须将我们要设置的配置 (

max_connection
) 作为部署中的
args
,例如:

args: ["-c", "max_connections=200"]
© www.soinside.com 2019 - 2024. All rights reserved.