不允许"root"执行PostgreSQL服务器

cho*_*on4 17 postgresql privileges sudo

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

postgres
Run Code Online (Sandbox Code Playgroud)

postgres不知道在哪里可以找到服务器配置文件.
您必须指定--config-file或-D invocation选项或设置PGDATA环境变量.

那么我尝试设置我的配置文件:

postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

嗯好的 接下来,我尝试执行与管理员相同的操作:

 sudo postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

不允许"root"执行PostgreSQL服务器.
必须在非特权用户ID下启动服务器,以防止可能的系统安全性受损.有关如何正确启动服务器的详细信息,请参阅文档.

我搜索了该错误消息,但无法找到解决方案.
任何人都可以对此提供一些见解吗?

Mut*_*mar 20

对于那些尝试使用官方docker镜像运行自定义命令的用户,请使用以下命令.docker-entrypoint.sh处理切换用户和处理其他权限.

docker-entrypoint.sh -c 'shared_buffers=256MB' -c 'max_connections=200'
Run Code Online (Sandbox Code Playgroud)

  • 好的,首先 `docker exec <container_id> -it bash`,然后在这个答案中输入命令。 (2认同)

Erw*_*ter 19

你的命令不符合你的想法.以系统用户 身份运行postgres:

 sudo -u postgres command
Run Code Online (Sandbox Code Playgroud)

要运行命令(也命名为postgres!):

sudo -u postgres postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

你的命令恰恰相反:

sudo postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

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

如果要以系统用户身份运行几个命令,请使用以下命令postgres更改用户:

sudo -u postgres -i
Run Code Online (Sandbox Code Playgroud)

...... exit当你完成的时候

如果您在以系统用户身份运行时看到此错误消息postgres,则对文件或其中一个包含目录的权限有问题.

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

结束时:


hej*_*oaz 8

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
Run Code Online (Sandbox Code Playgroud)

创建配置/地图:

kubectl create configmap custom-postgresql-conf --from-file=custom-postgresql.conf
Run Code Online (Sandbox Code Playgroud)

请注意,自定义设置中的值是根据 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: /sf/ask/1981827781/
          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
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式申请

kubectl apply -f postgres.yml
Run Code Online (Sandbox Code Playgroud)

转到您的 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;
Run Code Online (Sandbox Code Playgroud)

谢谢穆图库玛

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