Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql raises 'data directory has wrong ownership' when trying to use volume

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!

like image 484
lyapun Avatar asked Jul 04 '14 07:07

lyapun


People also ask

What is Pgdata?

Traditionally, the configuration and data files used by a database cluster are stored together within the cluster's data directory, commonly referred to as PGDATA (after the name of the environment variable that can be used to define it). A common location for PGDATA is /var/lib/pgsql/data .


1 Answers

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!

like image 181
lyapun Avatar answered Nov 08 '22 11:11

lyapun