I had postgres 11 installed using docker-compose. I wanted to upgrade it to 12 but even though I have removed the container and its volume but the status of the container says «Restarting».
Here is my docker-compose file
version: '3.5'
services:
postgres:
image: postgres:12
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
ports:
- "5432"
restart: always
volumes:
- /etc/postgresql/12/postgresql.conf:/var/lib/postgresql/data/postgresql.conf
- db_data:/var/lib/postgresql/data
volumes:
db_data:
However it is not working and the logs has the following issue
2020-07-02T12:54:47.012973448Z The files belonging to this database system will be owned by user "postgres".
2020-07-02T12:54:47.013030445Z This user must also own the server process.
2020-07-02T12:54:47.013068962Z
2020-07-02T12:54:47.013222608Z The database cluster will be initialized with locale "en_US.utf8".
2020-07-02T12:54:47.013261425Z The default database encoding has accordingly been set to "UTF8".
2020-07-02T12:54:47.013281815Z The default text search configuration will be set to "english".
2020-07-02T12:54:47.013293326Z
2020-07-02T12:54:47.013303793Z Data page checksums are disabled.
2020-07-02T12:54:47.013313919Z
2020-07-02T12:54:47.013450079Z initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
2020-07-02T12:54:47.013487706Z If you want to create a new database system, either remove or empty
2020-07-02T12:54:47.013501126Z the directory "/var/lib/postgresql/data" or run initdb
2020-07-02T12:54:47.013512379Z with an argument other than "/var/lib/postgresql/data".
How could I remove or empty this /var/lib/postgresql/data when the container is constantly restarting?
Thanks in advance
asked Jul 2, 2020 at 13:06
3
Quoting @yosifkit from this issue
The volume needs to be empty or a valid already initialized postgres
database with the filePG_VERSION
in there so the init can be
skipped.… If there are any files or folders in there like
lost+found
it
will probably fail to initialize. If there are files that you want to
keep in the volume (or have no control over) you could adjust the
PGDATA
environment variable to point to a sub-directory in there
like-e PGDATA=/var/lib/postgresql/data/db-files/
.
So I added PGDATA
to the environment
section of the compose file to solve the issue (notice the some_name
at the end):
services:
postgres:
image: postgres:12
environment:
PGDATA: /var/lib/postgresql/data/some_name/
answered Sep 10, 2020 at 7:21
pmsoltanipmsoltani
8308 silver badges13 bronze badges
2
I got this issue because the /var/lib/postgresql/data/postgresql.conf
and the /var/lib/postgresql/data
overlap in the docker container at /var/lib/postgresql/
.
An example of a broken config is:
version: "3.8"
services:
db:
image: "postgres:10"
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/var/lib/postgresql/data/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
To avoid this I tell PostgreSQL to find it’s config in /etc/postgresql.conf
instead so the overlapping volumes don’t occur like this:
version: "3.8"
services:
db:
image: "postgres:10"
command: ["postgres", "-c", "config_file=/etc/postgresql.conf"]
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/etc/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
This is similar to what pmsoltani suggests, but I move the location of the postgresql.conf
file instead of the data directory.
answered Nov 23, 2021 at 10:59
rjdkolbrjdkolb
9,91111 gold badges66 silver badges82 bronze badges
I had the same issue today, I fixed it by removing the content of the volume db_data in your case
docker volume ls
docker volume inspect db_data <-- will show you the mountpoint
I went to the directory (mountpoint) ex: /path/data
cp data data.backup
cd data
rm -R *
And start services:
docker-compose up -d
answered Feb 5, 2021 at 15:24
MraimouMraimou
1773 silver badges13 bronze badges
Another solution is to simply remove the volume attached to the container:
docker-compose down -v
answered Jan 27, 2021 at 16:52
2
Comments
rija
pushed a commit
to rija/gigadb-website
that referenced
this issue
Aug 12, 2018
rija
pushed a commit
to rija/gigadb-website
that referenced
this issue
Sep 4, 2018
atc0005
added a commit
to atc0005/cachet-docker
that referenced
this issue
Apr 4, 2019
urubens
added a commit
to Cytomine-ULiege/Cytomine-bootstrap
that referenced
this issue
Jun 26, 2019
jtb0
mentioned this issue
Jul 10, 2019
loum
added a commit
to loum/data-pipelines-infrastructure
that referenced
this issue
Apr 23, 2020
data-pipelines-infrastructure/docker-compose-celery.yml docker-compose.yml * fix PostgreSQL initdb issue in K8s as per docker-library/postgres#263 * add "user" setting so that "airflow" can write configuration K8s volumes k8s/init-db-pod.yaml k8s/postgres-deployment.yaml * updated K8s manifests as part of docker-compose mods
Всем привет! Пытаюсь связать volume внутри контейнера с внешней директорией при помощи вот такой вот команды.
docker run --name PSTGRS -p 5432:5432 -v /home/****/Desktop/pagila-master:/var/lib/postgresql/data -e GRANT_SUDO=yes -e POSTGRES_PASSWORD=password postgres
Но получаю ошибку:
initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgresql/data" or run initdb
with an argument other than "/var/lib/postgresql/data".
Команда docker inspect PSTGRS
выдаёт:
"Mounts": [
{
"Type": "bind",
"Source": "/home/***/Desktop/pagila-master",
"Destination": "/var/lib/postgresql/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
Я уже docker system purge пробовал и что я только не пробовал. Каждый раз эта ошибка.
Подскажите, пожалуйста, что я делаю не правильно?
Заранее большое спасибо!
I am trying to create the postgresql database.
When I install PostgreSQL, I gave this command:
sudo yum install postgresql postgresql-server
and later I modified the config file:
sudo vim /var/lib/pgsql/data/pg_hba.conf
and modified as
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 md5
While I am trying to startup the postgresql service:
sudo service postgresql initdb
> Data directory is not empty! [FAILED]
sudo chkconfig postgresql on
sudo service postgresql start
Starting postgresql service: [ OK ]
What is caused these errors and how do I fix them?
asked May 8, 2013 at 8:14
Initdb should only be run once. It will create the directory where you’ll keep the config files and (usually) the actual database. You’ve obviously already done that already; otherwrise there wouldn’t be any pg_hba.conf for you to edit.
So, just don’t run postgresql initdb
again, unless you are doing a complete reinstall.
answered May 8, 2013 at 8:35
Jenny DJenny D
27.5k21 gold badges74 silver badges112 bronze badges
3
From here:
If you’re completely wiping & reinstalling a Postgres DB, when running initdb
like:
service postgresql-9.2 initdb -E 'UTF8' --pgdata="/foo/bar/"
you can encounter this service error:
Data directory is not empty! [FAILED]
To fix it (and this is the nuclear option — all db data is wiped!)
On Amazon Linux (2014-x):
rm -rf /var/lib/pgsql9/data
On CentOS (6.x)
rm -rf /var/lib/pgsql/9.2/data
Now try the initdb
command again and it should work this time:
service postgresql-9.2 initdb
answered Jan 18, 2016 at 4:02
0
On systemd based systems like RHEL/CentOS 7 and Fedora the procedure for running initdb is somewhat different. This is no longer done by the init scripts (which no longer exist), and the new procedure is much closer to the upstream instructions.
You must first su
to the postgres
user, and then run initdb
or pg_ctl initdb
. It’s not necessary to provide a data directory if you are using a Red Hat build as its default automatically chooses the default data directory /var/lib/pgsql
.
For example:
# su - postgres
$ pg_ctl initdb
$ exit
#
Of course, you only do this once, on first installation, to set up the initial data directory. You would not do it again unless you were creating a completely new installation or restoring from a disaster.
answered Apr 21, 2015 at 3:03
Michael HamptonMichael Hampton
240k42 gold badges488 silver badges954 bronze badges
1
I had the same issue, using PostgreSQL 9.3 on CentOS 6.
I deleted the /var/lib/pgsql/9.3/data folder, then re-ran the command
sudo service postgresql-9.3 initdb
… which successfully initialised the db service again.
answered Oct 12, 2014 at 10:28
I recently started using arch. (arch version 5.6.4).
I installed postgresql by running:
sudo pacman -S postgresql
I’m following archwiki guide to initialize the database cluster. I switch to the postgres user by running
sudo -iu postgres
After which, I run:
initdb -D /var/lib/postgres/data
I get the following output:
[[email protected] ~]$ initdb -D /var/lib/postgres/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: error: directory "/var/lib/postgres/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/var/lib/postgres/data" or run initdb
with an argument other than "/var/lib/postgres/data".
I figured it wouldn’t be wise to remove everything in data, so I made a directory /home/user/postgres/data. After creating this directory, I made postgres its user.
I still get a permissions error when running initdb. I even changed permissions to allow read,write,execute but I can’t get past the permission error.
Help?
These are pretty much the steps I have followed in order. Basically what is outlined in the documentation:
https://docs.microsoft.com/en-us/azure/aks/azure-files-dynamic-pv
azure-storage-claim.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: test-app-sc
provisioner: kubernetes.io/azure-file
mountOptions:
- dir_mode=0777
- file_mode=0777
- uid=1000
- gid=1000
- mfsymlinks
- nobrl
- cache=none
parameters:
skuName: Standard_LRS
location: westus
azure-storage.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-app-storage
spec:
accessModes:
- ReadWriteMany
storageClassName: test-app-sc
resources:
requests:
storage: 15Gi<br>
PVC is now setup.
Changed the mountPath per the Postgres image documentation:
PGDATA
This optional variable can be used to define another location — like a subdirectory — for the database files. The default is /var/lib/postgresql/data, but if the data volume you’re using is a filesystem mountpoint (like with GCE persistent disks), Postgres initdb recommends a subdirectory (for example /var/lib/postgresql/data/pgdata ) be created to contain the data.
This is an environment variable that is not Docker specific. Because the variable is used by the postgres server binary (see the PostgreSQL docs), the entrypoint script takes it into account.
Based on that, I have my postgres.yaml
setup like the following:
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
spec:
replicas: 1
selector:
matchLabels:
component: postgres
template:
metadata:
labels:
component: postgres
spec:
containers:
- name: postgres
image: postgres
# I don't know, someone suggested this, but doesn't work apparently
securityContext:
runAsUser: 0
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: test-app-secrets
key: PGDATABASE
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: test-app-secrets
key: PGUSER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: test-app-secrets
key: PGPASSWORD
- name: POSTGRES_INITDB_ARGS
value: "-A md5"
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data/pgdata
subPath: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: test-app-storage
---
apiVersion: v1
kind: Service
metadata:
name: postgres-cluster-ip-service
spec:
type: ClusterIP
selector:
component: postgres
ports:
- port: 1423
targetPort: 5432
You get the error:
chmod: changing permissions of '/var/lib/postgresql/data/pgdata': Operation not permitted
So with either of that as the Dockerfile:
FROM postgres:11-alpine
EXPOSE 5432
RUN /bin/bash -c 'chmod 777 /var/lib/postgresql/data'
RUN /bin/bash -c 'chmod 777 /var/lib/postgresql/data/pgdata'
Or
FROM postgres:11-alpine
EXPOSE 5432
It doesn’t really matter, you still get the same type of error by doing any the following:
...
- name: POSTGRES_INITDB_ARGS
value: "-A md5"
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data/pgdata
subPath: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: test-app-storage
...<br>
Results in the following error:
The files belonging to this database system will be owned by user "postgres". This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: error: directory "/var/lib/postgresql/data" exists but is not empty If you want to create a new database system, either remove or empty the directory "/var/lib/postgresql/data" or run initdb with an argument other than "/var/lib/postgresql/data".
Try this:
...
- name: POSTGRES_INITDB_ARGS
value: "-A md5"
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
subPath: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: test-app-storage
...
And it results in this:
chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
Try this:
...
- name: POSTGRES_INITDB_ARGS
value: "-A md5"
value: "-D /var/lib/postgresql/data/pgdata"
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data/pgdata
subPath: postgres-storage
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: test-app-storage
...
And it results in this:
The files belonging to this database system will be owned by user "postgres". This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english".
Data page checksums are disabled.
initdb: error: could not change permissions of directory "/var/lib/postgresql/data/pgdata": Operation not permitted fixing permissions on existing directory /var/lib/postgresql/data/pgdata ...
So nothing seems to work that I've tried and following the documentation where I can.
Someone suggested to get rid of the volume mounts like so:
...
- name: POSTGRES_INITDB_ARGS
value: "-A md5"
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: test-app-storage
...
Which, hey, that actually works! But doesn’t persist data given it just uses the Pod storage so is pretty pointless:
And sure enough when you create a table in Postgres, destroy the Pod, and then redeploy it, of course the table is no longer there.
So more than likely I’m doing something wrong, but I’ve been following the documentation and seems like this should work.
Where are things going wrong?
EDIT: Permissions in Pod
Apparently it is a permissions issue that occurs when PGDATA
is the same directory as mountPath
. For example:
...
- name: PGDATA
value: /var/lib/postgresql-data
volumeMounts:
- name: test-app-storage
mountPath: /var/lib/postgresql-data
subPath: postgres-storage
...
or
...
# if PGDATA is not specified it defaults to /var/lib/postgresql/data
# - name: PGDATA
# value: /var/lib/postgresql-data
volumeMounts:
- name: test-app-storage
mountPath: /var/lib/postgresql/data
subPath: postgres-storage
...
Something like this where they do not match will create the Pod, but uses Pod storage which I obviously don’t want:
# Thus /var/lib/postgresql/data
# - name: PGDATA
# value: /var/lib/postgresql-data
volumeMounts:
- name: test-app-storage
mountPath: /var/lib/postgresql-data
subPath: postgres-storage
Permissions ls -l
looks like this:
$ ls -l
drwxr-xr-x 1 root root 4096 Feb 2 06:06 apt
drwxr-xr-x 1 root root 4096 Feb 2 06:07 dpkg
drwxr-xr-x 2 root root 4096 Feb 2 06:06 exim4
drwxr-xr-x 2 root root 4096 Aug 28 2018 logrotate
drwxr-xr-x 2 root root 4096 Nov 10 12:17 misc
drwxr-xr-x 2 root root 4096 Jan 30 00:00 pam
drwxr-xr-x 1 postgres postgres 4096 Feb 2 06:07 postgresql
drwxrwxrwx 2 1000 1000 0 Jan 31 21:46 postgresql-data
drwxr-xr-x 1 root root 4096 Jan 30 00:00 systemd
drwxr-xr-x 3 root root 4096 Feb 2 06:07 ucf
$ ls -l postgresql && ls -l postgresql/data && ls -l postgresql-data
total 4
drwx------ 19 postgres postgres 4096 Feb 5 23:28 data
total 124
drwx------ 6 postgres postgres 4096 Feb 5 23:28 base
drwx------ 2 postgres postgres 4096 Feb 5 23:29 global
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_commit_ts
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_dynshmem
-rw------- 1 postgres postgres 4281 Feb 5 23:28 pg_hba.conf
-rw------- 1 postgres postgres 1636 Feb 5 23:28 pg_ident.conf
drwx------ 4 postgres postgres 4096 Feb 5 23:33 pg_logical
drwx------ 4 postgres postgres 4096 Feb 5 23:28 pg_multixact
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_notify
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_replslot
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_serial
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_snapshots
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_stat
drwx------ 2 postgres postgres 4096 Feb 5 23:51 pg_stat_tmp
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_subtrans
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_tblspc
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_twophase
-rw------- 1 postgres postgres 3 Feb 5 23:28 PG_VERSION
drwx------ 3 postgres postgres 4096 Feb 5 23:28 pg_wal
drwx------ 2 postgres postgres 4096 Feb 5 23:28 pg_xact
-rw------- 1 postgres postgres 88 Feb 5 23:28 postgresql.auto.conf
-rw------- 1 postgres postgres 26588 Feb 5 23:28 postgresql.conf
-rw------- 1 postgres postgres 36 Feb 5 23:28 postmaster.opts
-rw------- 1 postgres postgres 94 Feb 5 23:28 postmaster.pid
total 0
The permissions for where it creates the data files is postgres
. However, doing this, it doesn’t map to Azure Files and the PVC. It just stays and is destroyed with the Pod.
I think what is happening is mountPath
uses root
and PGDATA
uses postgres
, and somehow mountPath
is trying to use postgres
???
Really, not sure and still lost as to how to resolve it.
EDIT2
Came across this answer:
https://stackoverflow.com/a/51203031/3123109
So added the following to mine:
- name: postgres
image: postgres
command:
- /bin/chown
- -R
- "1000"
- /var/lib/postgresql/data
But this generates a new error:
The selected container has not logged any messages yet.
Progress, I guess.
У меня был установлен postgres 11 с помощью docker-compose. Я хотел обновить его до 12, но хотя я удалил контейнер и его объем, но статус контейнера говорит «Перезапуск».
Вот мой файл для создания докеров
version: '3.5'
services:
postgres:
image: postgres:12
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
ports:
- "5432"
restart: always
volumes:
- /etc/postgresql/12/postgresql.conf:/var/lib/postgresql/data/postgresql.conf
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Однако он не работает, и в журналах есть следующая проблема
2020-07-02T12:54:47.012973448Z The files belonging to this database system will be owned by user "postgres".
2020-07-02T12:54:47.013030445Z This user must also own the server process.
2020-07-02T12:54:47.013068962Z
2020-07-02T12:54:47.013222608Z The database cluster will be initialized with locale "en_US.utf8".
2020-07-02T12:54:47.013261425Z The default database encoding has accordingly been set to "UTF8".
2020-07-02T12:54:47.013281815Z The default text search configuration will be set to "english".
2020-07-02T12:54:47.013293326Z
2020-07-02T12:54:47.013303793Z Data page checksums are disabled.
2020-07-02T12:54:47.013313919Z
2020-07-02T12:54:47.013450079Z initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
2020-07-02T12:54:47.013487706Z If you want to create a new database system, either remove or empty
2020-07-02T12:54:47.013501126Z the directory "/var/lib/postgresql/data" or run initdb
2020-07-02T12:54:47.013512379Z with an argument other than "/var/lib/postgresql/data".
Как я могу удалить или очистить этот /var/lib/postgresql/data, когда контейнер постоянно перезапускается?
Заранее спасибо
4 ответа
Цитата @yosifkit из эта проблема
Том должен быть пустым или действительным, уже инициализированным postgres. база данных с файлом
PG_VERSION
, поэтому инициализация может быть пропущено.… Если там есть какие-либо файлы или папки, такие как
lost+found
, это вероятно, не удастся инициализировать. Если есть файлы, которые вы хотите сохранить громкость (или не иметь никакого контроля над), вы можете отрегулироватьPGDATA
переменная окружения, указывающая на подкаталог там как-e PGDATA=/var/lib/postgresql/data/db-files/
.
Поэтому я добавил PGDATA
в раздел environment
файла компоновки, чтобы решить проблему (обратите внимание на some_name
в конце):
services:
postgres:
image: postgres:12
environment:
PGDATA: /var/lib/postgresql/data/some_name/
23
pmsoltani
10 Сен 2020 в 10:21
У меня была такая же проблема сегодня, я исправил ее, удалив содержимое тома db_data в вашем случае.
docker volume ls
docker volume inspect db_data <-- will show you the mountpoint
Я пошел в каталог (точка монтирования) ex: /path/data
cp data data.backup
cd data
rm -R *
И запускаем сервисы:
docker-compose up -d
3
Mraimou
5 Фев 2021 в 18:24
Другое решение — просто удалить том, прикрепленный к контейнеру:
docker-compose down -v
-5
nicocharlery
27 Янв 2021 в 19:52
У меня возникла эта проблема, потому что /var/lib/postgresql/data/postgresql.conf
и /var/lib/postgresql/data
перекрываются в контейнере докера в /var/lib/postgresql/
.
Пример неисправной конфигурации:
version: "3.8"
services:
db:
image: "postgres:10"
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/var/lib/postgresql/data/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
Чтобы избежать этого, я говорю PostgreSQL найти свою конфигурацию в /etc/postgresql.conf
вместо этого, чтобы не возникало перекрывающихся томов:
version: "3.8"
services:
db:
image: "postgres:10"
command: ["postgres", "-c", "config_file=/etc/postgresql.conf"]
ports:
- "5432:5432"
volumes:
- ./postgresql.conf:/etc/postgresql.conf
- ./pg-data:/var/lib/postgresql/data
Это похоже на то, что предлагает pmsoltani, но я перемещаю местоположение файла postgresql.conf
вместо каталога данных .
6
rjdkolb
23 Ноя 2021 в 14:25