Docker: Error copying CSV file to container PostgreSQL - postgresql

I'm trying to create a docker container with a PostgreSQL database with already set tables and containig data. I will setup this db through a sqlfile and load this data with a .CSV file. My OS is windows and i'm using Visual Studio Code
Directory
-pg_data
|-- DATA.CSV
-.env
-docker-compose.yml
-init.sql
docker-compose.yml
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- DATABASE_HOST=${DB_HOST}
ports:
- '5432:5432'
volumes:
- ./pg_data:/var/lib/pg_data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
volumes:
pg_data:
init.sql
DROP TABLE IF EXISTS public."measurements" CASCADE;
CREATE TABLE public."measurements"
(
"measurementID" integer GENERATED ALWAYS AS IDENTITY,
tijd timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
geluid text NOT NULL,
co2 decimal NOT NULL,
temperatuur decimal NOT NULL,
luchtvochtigheid decimal NOT NULL,
voc decimal NOT NULL,
no2 decimal NOT NULL,
pm1_0 decimal NOT NULL,
pm2_5 decimal NOT NULL,
pm4_0 decimal NOT NULL,
pm10_0 decimal NOT NULL,
PRIMARY KEY ("measurementID")
);
\copy public."measurements" FROM '/var/lib/pgdata/DATA.CSV' DELIMITER ';' CSV;
Error
When i rundocker-compose up --build
the container gives back an error
db_1 | psql:/docker-entrypoint-initdb.d/init.sql:74: error: /var/lib/pgdata/DATA.CSV: No such file or directory
Tries:
I have tried moving the local pg_data and DATA.csv file to different places aswell as replacing my current volume to ./pg_data:/var/lib/postgresql/data/pgdata
This gave a different error:
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
db_1 | If you want to create a new database system, either remove or empty
db_1 | the directory "/var/lib/postgresql/data" or run initdb
db_1 | with an argument other than "/var/lib/postgresql/data".

Related

Create a postgres docker-compose with a local mount volume

I have the follow code of my docker-compose.yml:
version: '3'
services:
db:
image: postgres:14.6
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./data:/var/lib/postgresql/data
But when I do docker compose up, the console give me the next error:
servicios-basesdatos-db-1 exited with code 1
servicios-basesdatos-db-1 | The files belonging to this database system will be owned by user "postgres".
servicios-basesdatos-db-1 | This user must also own the server process.
servicios-basesdatos-db-1 |
servicios-basesdatos-db-1 | The database cluster will be initialized with locale "en_US.utf8".
servicios-basesdatos-db-1 | The default database encoding has accordingly been set to "UTF8".
servicios-basesdatos-db-1 | The default text search configuration will be set to "english".
servicios-basesdatos-db-1 |
servicios-basesdatos-db-1 | Data page checksums are disabled.
servicios-basesdatos-db-1 |
servicios-basesdatos-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
servicios-basesdatos-db-1 | creating subdirectories ... ok
servicios-basesdatos-db-1 | selecting dynamic shared memory implementation ... posix
servicios-basesdatos-db-1 | selecting default max_connections ... 20
servicios-basesdatos-db-1 | selecting default shared_buffers ... 400kB
servicios-basesdatos-db-1 | selecting default time zone ... Etc/UTC
servicios-basesdatos-db-1 | creating configuration files ... ok
servicios-basesdatos-db-1 | 2023-01-31 09:07:54.976 UTC [83] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership
servicios-basesdatos-db-1 | 2023-01-31 09:07:54.976 UTC [83] HINT: The server must be started by the user that owns the data directory.
servicios-basesdatos-db-1 | child process exited with exit code 1
servicios-basesdatos-db-1 | initdb: removing contents of data directory "/var/lib/postgresql/data"
And I can't up my docker.
How can I fix it so that it works?
Think this line is crucial here
servicios-basesdatos-db-1 | 2023-01-31 09:07:54.976 UTC [83] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership
You may try this solution (initialization of volume in volumes) like:
version: '2'
services:
db:
image: postgres:14.6
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
If you look at the docker file if the image here https://github.com/docker-library/postgres/blob/41bd7bf3f487e6dc0036fd73efaff6ccb6fbbacd/14/bullseye/Dockerfile you'll see that they create a user with id 999.
I guess your local data directory is owned by you, which is in most cases user 1000.
So if you chown the data directory to user 999, it should work.

Permissions error with docker compose on macOS

I'm trying to spin up a postgresl database using docker compose on my mac running macOS 12.4.
My docker-compose.yml file is
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: gymbro
ports:
- 5438:5432
volumes:
- ./pgdata:/var/lib/postgresql/data
Running docker-compose up results in the container exiting with status 1. Here is the relevant output
Attaching to docker-compose-gymbro-db-1
docker-compose-gymbro-db-1 | The files belonging to this database system will be owned by user "postgres".
docker-compose-gymbro-db-1 | This user must also own the server process.
docker-compose-gymbro-db-1 |
docker-compose-gymbro-db-1 | The database cluster will be initialized with locale "en_US.utf8".
docker-compose-gymbro-db-1 | The default database encoding has accordingly been set to "UTF8".
docker-compose-gymbro-db-1 | The default text search configuration will be set to "english".
docker-compose-gymbro-db-1 |
docker-compose-gymbro-db-1 | Data page checksums are disabled.
docker-compose-gymbro-db-1 |
docker-compose-gymbro-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
docker-compose-gymbro-db-1 | creating subdirectories ... ok
docker-compose-gymbro-db-1 | selecting dynamic shared memory implementation ... posix
docker-compose-gymbro-db-1 | selecting default max_connections ... 100
docker-compose-gymbro-db-1 | selecting default shared_buffers ... 128MB
docker-compose-gymbro-db-1 | selecting default time zone ... Etc/UTC
docker-compose-gymbro-db-1 | creating configuration files ... ok
docker-compose-gymbro-db-1 | running bootstrap script ... ok
docker-compose-gymbro-db-1 | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL: data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1 | 2022-12-24 17:42:20.791 UTC [40] DETAIL: Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
docker-compose-gymbro-db-1 | child process exited with exit code 1
docker-compose-gymbro-db-1 | initdb: removing contents of data directory "/var/lib/postgresql/data"
docker-compose-gymbro-db-1 exited with code 1
Seems like there is a permissions issue,
docker-compose-gymbro-db-1 | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL: data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1 | 2022-12-24 17:42:20.791 UTC [40] DETAIL: Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
I can spin up the db using a docker volume, but I'm just curious as to why this is happening and if there is a possible solution.
On Linux here, so this might not work on Mac. However, why not avoid using the local volum?
Like:
version: '3'
services:
db:
image: postgres:13.2
env_file:
- .env
volumes:
- data:/var/lib/postgresql/data
volumes:
data: {}
This is a solution I always use and solved my issues once and for all when I used to have it.
However, there is a more interesting discussion on this here that you can go through: https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/22
I have seen some items I honestly won't recommend like changing the PG_DATA, but if you're comfortable with it, have a go.
Let me know what you think :)

Docker Composed PostgreSQL Copy Functioning but No Relations Found in Database

I have a simple Dockerfile that sets up a database and then runs a .sql file. The image builds and container starts with no apparent issue, but PostgreSQL is missing data that should have been included.
Here's the setup, logs, and problem---
Dockerfile
FROM postgres
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
ENV POSTGRES_DB exampledb
COPY db-setup.sql /docker-entrypoint-initdb.d/
I run it with docker build -t example-db ./
This is the copied .sql file
BEGIN;
CREATE TABLE IF NOT EXISTS users (
id integer NOT NULL,
name VARCHAR,
email VARCHAR,
password VARCHAR,
remember_token varchar(100),
company_name VARCHAR,
company_street VARCHAR,
company_city VARCHAR,
company_zipcode integer,
created_at timestamp,
updated_at timestamp
);
CREATE TABLE IF NOT EXISTS quotes (
id integer NOT NULL,
dot_number integer,
nbr_of_power_units integer,
value_of_power_units integer,
premium_amount integer,
premium_tax integer,
premium_total integer,
street VARCHAR,
city VARCHAR,
state VARCHAR,
zipcode integer,
driver1_name VARCHAR,
driver1_age integer,
driver2_name VARCHAR,
driver2_age integer,
driver3_name VARCHAR,
driver3_age integer,
driver4_name VARCHAR,
driver4_age integer,
driver5_name VARCHAR,
driver5_age integer,
created_at timestamp,
updated_at timestamp
);
INSERT INTO users (id, name) VALUES (1, 'test-user');
Lastly I start the container via docker run --name example-container -p 5432:5432 example-db
It appears to function correctly, outputing this log.
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.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
ok
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2021-05-07 04:04:45.428 UTC [47] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-05-07 04:04:45.429 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-05-07 04:04:45.435 UTC [48] LOG: database system was shut down at 2021-05-07 04:04:45 UTC
2021-05-07 04:04:45.439 UTC [47] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/db-setup.sql
BEGIN
CREATE ROLE
CREATE TABLE
CREATE TABLE
INSERT 0 1
waiting for server to shut down....2021-05-07 04:04:45.772 UTC [47] LOG: received fast shutdown request
2021-05-07 04:04:45.774 UTC [47] LOG: aborting any active transactions
2021-05-07 04:04:45.776 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
2021-05-07 04:04:45.777 UTC [49] LOG: shutting down
2021-05-07 04:04:45.810 UTC [47] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2021-05-07 04:04:45.919 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-05-07 04:04:45.919 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2021-05-07 04:04:45.920 UTC [1] LOG: listening on IPv6 address "::", port 5432
2021-05-07 04:04:45.923 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-05-07 04:04:45.929 UTC [84] LOG: database system was shut down at 2021-05-07 04:04:45 UTC
2021-05-07 04:04:45.935 UTC [1] LOG: database system is ready to accept connections
Pulling from that log there's an indication that the tables in the .sql file were created, and the test row inserted.
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/db-setup.sql
BEGIN
CREATE ROLE
CREATE TABLE
CREATE TABLE
INSERT 0 1
However, when I bash into the Docker container docker exec -it example-container /bin/bash and then log into psql psql -U postgres
I can see the newly created database
------------+----------+----------+------------+------------+-----------------------
exampledb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
But, connecting to the db and running \dt yields- "Did not find any relations."
I am very confused and any help would be appreciated.
Ah! Man...this one looks sneaky. I think your culprit is the BEGIN; at the start of your SQL init script. That starts a Postgres transaction. Essentially that means your script will create your tables, but since there isn't an END; statement, the Postgres transaction is left open.
When Docker aborts active transactions as a part of its startup process, that init script transaction is aborted. And when an in progress Postgres transaction is aborted, all the work inside that transaction block is reversed. I.e., you go back to the state your database was in when the init script started - empty.
Try removing the BEGIN; and running it from there!

Docker-Compose + Postgres: /docker-entrypoint-initdb.d/init.sql: Permission denied

I have the following docker compose file:
version: "3"
services:
postgres:
image: postgres:11.2-alpine
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
ports:
- "5432:5432"
volumes:
- ./init-db/init-db.sql:/docker-entrypoint-initdb.d/init.sql
This is the init-db.sql:
CREATE TABLE users (
email VARCHAR(355) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
price NUMERIC(6, 2) NOT NULL,
category INT NOT NULL
);
INSERT INTO users VALUES ('test#test.com', 'Test*123');
INSERT INTO products (title, price, category) VALUES ('Truco', 9.90, 13);
When I run docker-compose up, I'm getting this error:
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
/docker-entrypoint-initdb.d/init.sql: Permission denied
I already tried to:
chmod 777 on the sql file
chmod -x on the sql file
Run docker and docker-compose using sudo
Any idea?
I found the easy way to solve this...
You should use "build" way to create postgres service
And DO NOT setting the volume for init.sql, it will cause the permission problem.
postgres:
build: ./postgres
Create a Dockerfile for postgres like this
FROM postgres:12
COPY ./init.sql /docker-entrypoint-initdb.d/init.sql
CMD ["docker-entrypoint.sh", "postgres"]
Then it should works out.
Hope my answer would help you!
For me problem was in my machine. enabled SELinux access control, which did not allow for containers to expand files.
Solution, disable SELinux:
echo SELINUX=disabled > /etc/selinux/config
SELINUXTYPE=targeted >> /etc/selinux/config
setenforce 0
From this
I had a similar problem when using the ADD command.
When using ADD (eg to download a file) the default chmod value is 711. When using the COPY command the chmod will match the hosts chmod of the file where you copy it from. The solution is to set the permission before copying, or change them in your Dockerfile after they've been copied.
It looks like there will finally be a "COPY --chmod 775" flag available in the upcoming docker 20 which will make this easier.
https://github.com/moby/moby/issues/34819
When the sql file in /docker-entrypoint-initdb.d/ has a permission of 775 then the file is run correctly.
You can test this within the image (override entrypoint to /bin/bash) using:
docker-entrypoint.sh postgres
I have same problem on my macOS, but it's OK on my ubuntu laptop.
I found the problem is that MacOS cannot let docker access the folder.
Following link may resolve your problem.
https://stackoverflow.com/a/58482702/10752354
Besides, in my case, I should add one line code in my init.sql file because the default database is "root", and I should change "root" database into "postgres" database, or in your case for another custom database instead of root.
> docker-compose exec db psql -U root
psql (13.0 (Debian 13.0-1.pgdg100+1))
Type "help" for help.
root=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privilege
s
-----------+-------+----------+------------+------------+-----------------
--
postgres | root | UTF8 | en_US.utf8 | en_US.utf8 |
root | root | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | root | UTF8 | en_US.utf8 | en_US.utf8 | =c/root
+
| | | | | root=CTc/root
template1 | root | UTF8 | en_US.utf8 | en_US.utf8 | =c/root
+
| | | | | root=CTc/root
(4 rows)
so I need to add \c postgres in my init.sql file:
\c postgres // for creating table in the database named 'postgres'
create table sample_table. ( ... )
I tried with the following compose file and it seems working as expected. are you sure form the path of the files you use?
version: "3"
services:
postgres:
image: postgres:11.2-alpine
environment:
POSTGRES_PASSWORD: root
POSTGRES_USER: root
ports:
- "5432:5432"
volumes:
- ./init-db.sql:/docker-entrypoint-initdb.d/init.sql
files structure
drwxr-xr-x 4 shihadeh 502596769 128B Feb 28 22:37 .
drwxr-xr-x 12 shihadeh 502596769 384B Feb 28 22:36 ..
-rw-r--r-- 1 shihadeh 502596769 244B Feb 28 22:37 docker-compose.yml
-rw-r--r-- 1 shihadeh 502596769 380B Feb 28 22:37 init-db.sql
output of docker-compose up
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |
postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
postgres_1 | The default database encoding has accordingly been set to "UTF8".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1 | creating subdirectories ... ok
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting dynamic shared memory implementation ... posix
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... sh: locale: not found
postgres_1 | 2020-02-28 21:45:01.363 UTC [26] WARNING: no usable system locales were found
postgres_1 | ok
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 |
postgres_1 | WARNING: enabling "trust" authentication for local connections
postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1 | --auth-local and --auth-host, the next time you run initdb.
postgres_1 | waiting for server to start....2020-02-28 21:45:02.272 UTC [30] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-02-28 21:45:02.294 UTC [31] LOG: database system was shut down at 2020-02-28 21:45:01 UTC
postgres_1 | 2020-02-28 21:45:02.299 UTC [30] LOG: database system is ready to accept connections
postgres_1 | done
postgres_1 | server started
postgres_1 | CREATE DATABASE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
postgres_1 | CREATE TABLE
postgres_1 | CREATE TABLE
postgres_1 | INSERT 0 1
postgres_1 | INSERT 0 1
postgres_1 |
postgres_1 |
postgres_1 | waiting for server to shut down....2020-02-28 21:45:02.776 UTC [30] LOG: received fast shutdown request
postgres_1 | 2020-02-28 21:45:02.779 UTC [30] LOG: aborting any active transactions
postgres_1 | 2020-02-28 21:45:02.781 UTC [30] LOG: background worker "logical replication launcher" (PID 37) exited with exit code 1
postgres_1 | 2020-02-28 21:45:02.781 UTC [32] LOG: shutting down
postgres_1 | 2020-02-28 21:45:02.826 UTC [30] LOG: database system is shut down
postgres_1 | done
postgres_1 | server stopped
postgres_1 |
postgres_1 | PostgreSQL init process complete; ready for start up.
postgres_1 |
postgres_1 | 2020-02-28 21:45:02.890 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-02-28 21:45:02.890 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-02-28 21:45:02.895 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-02-28 21:45:02.915 UTC [43] LOG: database system was shut down at 2020-02-28 21:45:02 UTC
postgres_1 | 2020-02-28 21:45:02.921 UTC [1] LOG: database system is ready to accept connections
Just adding my solution even when it is a little late:
My problem:
I am installing the source files from an udemy course:
https://github.com/rockthejvm/spark-essentials
We have 2 important things here:
Docker compose file: docker-compose.yml
Folder and sql file to do db initialization: ./sql/bd.sql
I downloaded the zip file to my Ubuntu 20.04
When doing docker-compose up, I got the error:
Attaching to postgres
postgres | ls: cannot open directory '/docker-entrypoint-initdb.d/': Permission denied
postgres exited with code 2
SOLUTION
Before running doccker-compose up do:
run chmod 777 ON FOLDER called sql that contains file db.sql
chmod 777 sql
Explanation:
The folder called sql contains file db.sql to init the database from inside the docker container so, we need to give the permissions to run the file inside folde called sql.
I think you only need to do as below.
Give permisions to your folder:
chmod 777 init-db
be sure your sql file can be read by all users:
chmod 666 init-db.sql

PostgreSQL with docker ownership issue

Since yesterday, I try to launch my docker project on Windows (Bootcamp MacBook Pro), and I have just one issue left : PostgreSQL image.
Error message :
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |
postgres_1 | The database cluster will be initialized with locale "en_US.utf8".
postgres_1 | The default database encoding has accordingly been set to "UTF8".
postgres_1 | The default text search configuration will be set to "english".
postgres_1 |
postgres_1 | Data page checksums are disabled.
postgres_1 |
postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1 | creating subdirectories ... ok
postgres_1 | selecting default max_connections ... 20
postgres_1 | selecting default shared_buffers ... 400kB
postgres_1 | selecting dynamic shared memory implementation ... posix
postgres_1 | creating configuration files ... ok
postgres_1 | 2019-01-22 16:57:37.016 UTC [79] FATAL: data directory "/var/lib/postgresql/data" has wrong ownership
postgres_1 | 2019-01-22 16:57:37.016 UTC [79] HINT: The server must be started by the user that owns the data directory.
postgres_1 | child process exited with exit code 1
postgres_1 | initdb: removing contents of data directory "/var/lib/postgresql/data"
postgres_1 | running bootstrap script ... kibati-docker_postgres_1 exited with code 1
I've searched everywhere, tried everything, and still have this issue…
What I tried is :
Create docker volume with docker volume create --name postgres -d local and use it in my docker-compose.yml
docker-compose.yml
version: '2'
services:
postgres:
image: postgres:latest
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
networks:
internal_ip:
ipv4_address: 192.168.1.2
volumes:
postgres:
external: true
Add volume directly in docker compose
volume sample
volumes:
postgres:
driver: local
Using relative or absolute Windows paths
Declare multiple volumes for differents PostgreSQL folders (conf, data…)
I tried to docker compose down, restart computer, remove images, nothing change, same error.
I already checked Shared Drive box in Docker Settings.
Sources of what I've tried :
https://glennsarti.github.io/blog/puppet-in-docker/
https://forums.docker.com/t/trying-to-get-postgres-to-work-on-persistent-windows-mount-two-issues/12456/5
https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/28
https://github.com/docker-library/postgres/issues/435
http://www.lukaszewczak.com/2016/09/run-postgresql-using-docker-with.html
https://gdevops.gitlab.io/tuto_docker/tutoriels/postgresql/postgresql.html
https://devask.cz/questions/48645804/mount-postgres-data-to-windows-host-directory
Is anyone as a working solution ? I will continue to make it work, and update post if I found Something.
Thanks in advance.
I've finally figured out what went wrong when I tried to use a volume for PostgreSQL data.
I had no idea that we used a docker-compose.override.yml, which declare a volume with a Windows path.
So here is a working solution to have PostgreSQL on Docker for Windows, with persisted data :
version: '2'
services:
postgres:
image: postgres:11.5
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
- pgconf:/etc/postgresql
- pglog:/var/log/postgresql
volumes:
pgdata:
driver: local
pgconf:
driver: local
pglog:
driver: local
(no additional command required)
container_name: postgresql
image: postgres:latest
environment:
POSTGRES_PASSWORD: postgres
# volumes:
# - "./database:/var/lib/postgresql/data/"
ports:
- "5432:5432"
Comment the volumes
This work around did the trick and worked on windows, i have to bring those two lines back on mac but that's the easiest way to work for now until some real solution get pushed by docker windows or posgres docker