14

I'm trying to run postgresql in docker container, but of course I need to have my database data to be persistent, so I'm trying to use data only container which expose volume to store database at this place.

So, my data container has such Dockerfile:

FROM ubuntu

# Create data directory
RUN mkdir -p /data/postgresql

# Create /data volume
VOLUME /data/postgresql

Which I run:

docker run --name postgresql_data lyapun/postgresql_data true

In my postgresql.conf I set:

data_directory = '/data/postgresql'

Then I run my postgresql container in such way:

docker run -d --name postgre --volumes-from postgresql_data lyapun/postgresql

And I got:

2014-07-04 07:45:57 GMT FATAL:  data directory "/data/postgresql" has wrong ownership
2014-07-04 07:45:57 GMT HINT:  The server must be started by the user that owns the data directory.

How to deal with this issue? I googled a lot to find some information about using postgresql with docker volumes, but I didn't found anything.

Thanks!

lyapun
  • 500
  • 1
  • 3
  • 11
  • How are you starting postgres? – Marcus Hughes Jul 04 '14 at 08:31
  • CMD ["/usr/lib/postgresql/9.1/bin/postgres", "-D", "/var/lib/postgresql/9.1/main", "-c", "config_file=/etc/postgresql/9.1/main/postgresql.conf"] – lyapun Jul 04 '14 at 08:38
  • Are you running that as the user, `postgres`? – Marcus Hughes Jul 04 '14 at 08:41
  • yes. USER postgres EXPOSE 5432 CMD ["/usr/lib/postgresql/9.1/bin/postgres", "-D", "/var/lib/postgresql/9.1/main", "-c", "config_file=/etc/postgresql/9.1/main/postgresql.conf"] – lyapun Jul 04 '14 at 08:45
  • In my case, I had the data directory mounted on an external hard disk. File format was different in the external hard disk. I started giving the mount directory to the home location and it worked. – nizam.sp Dec 13 '16 at 05:52

4 Answers4

6

Ok, seems like I found workaround for this issue.

Instead of running postgres in such way:

CMD ["/usr/lib/postgresql/9.1/bin/postgres", "-D", "/var/lib/postgresql/9.1/main", "-c", "config_file=/etc/postgresql/9.1/main/postgresql.conf"]

I wrote bash script:

chown -Rf postgres:postgres /data/postgresql
chmod -R 700 /data/postgresql
sudo -u postgres /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf

And replaced CMD in postgresql image to:

CMD ["bash", "/run.sh"]

It works!

lyapun
  • 500
  • 1
  • 3
  • 11
  • 3
    Are you sure? I notice that you are still using `/var/lib/postgresql/9.1/main` as your data folder and not the mounted volume `/data/postgresql`. So the data is still being saved in the container. – picardo Jan 07 '15 at 17:47
2

This type of errors is quite common when you link a NTFS directory into your docker container. NTFS directories don't support ext3 file & directory access control. The only way to make it work is to link directory from a ext3 drive into your container.

I got a bit desperate when I played around Apache / PHP containers with linking the www folder. After I linked files reside on a ext3 filesystem the problem disappear.

I published a short Docker tutorial on youtube, may it helps to understand this problem: https://www.youtube.com/watch?v=eS9O05TTFjM

Elmar Dott
  • 47
  • 9
  • The video is not available anymore... – egvo Jul 10 '23 at 09:13
  • Your answer is correct. I moved my docker to ntfs and it denied to run postgres on it. So I had to change my volume's mount point to ext3 device directory. This asnwer helped me to finish the investigation: https://stackoverflow.com/a/55952189/7744106 – egvo Jul 10 '23 at 09:46
0

You have to set ownership of directory /data/postgresql to the same user, under which you are running your postgresql binary. For example, in Ubuntu it is usually postgres user.

Then you have to use this command:

chown postgres.postgres /data/postgresql
Jan Marek
  • 10,390
  • 3
  • 21
  • 19
  • In which Dockerfile should I do this? I tried and this didn't help – lyapun Jul 04 '14 at 08:17
  • @lyapun to be honest, I don't know docker. But I think, that you can do that, when you creating a volume... – Jan Marek Jul 04 '14 at 08:21
  • thanks but user postgres created in container which expose volume will be different to user postgres which created in postgresql container. – lyapun Jul 04 '14 at 08:27
  • 1
    @lyapun try to run command `id postgres` in the same container, as you run `postgresql` in it. You will see numerical uid and gid. Then use this numerical uid/gid instead of `postgres` values in the `chown` command. – Jan Marek Jul 04 '14 at 09:53
0

A better way to solve that issue, assuming your postgres images is named "postgres" and that your backup is ./backup.tar:

First, add this to your postgres Dockerfile:

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

Then run:

docker run -it --name postgres -v $(pwd):/db postgres sh -c "tar xvf /db/backup.tar --no-overwrite-dir" && \
docker run -it --name data --volumes-from postgres busybox true && \
docker rm postgres && \
docker run -it --name postgres --volumes-from=data postgres

You don't have permission issues since the archive is extracted by the postgres user of your postgres image, so it is the owner of the extracted files. You can then backup your data using the data container. The advantage of this solution is that you don't chmod/chown every time you run the image.

pkowalczyk
  • 16,763
  • 6
  • 27
  • 35
Carl Levasseur
  • 4,203
  • 3
  • 19
  • 19