Hom*_*lli 6 postgresql psql docker docker-compose
我已经浪费了整整一天的时间,并且说我不会对应该是一项简单任务的不必要的复杂性留下深刻的印象 - 这将是一种严重的轻描淡写.
好吧,有了这个,我正在使用docker-machine,docker-compose,postgresql和redis构建一个django应用程序 - 按照本教程.
我已经设法让基本教程工作 - 但它不符合我的需要,因为我需要为我的应用程序创建用户和数据库 - 而不是使用'postgres'.
我使用了@dnephin的答案来解决类似的问题,并修改了我的代码如下:
我在新目录中创建了一个新的Dockerfile ./database/
:
FROM postgres:9.6
COPY . /fixtures
WORKDIR /fixtures
RUN /fixtures/setup.sh
Run Code Online (Sandbox Code Playgroud)
./database/setup.sh
内容:
#!/bin/bash
set -e
pg_createcluster 9.6 main --start
/etc/init.d/postgresql start
su - postgres # makes no effing difference ...
psql -f create_fixtures.sql
/etc/init.d/postgresql stop
Run Code Online (Sandbox Code Playgroud)
./database/create_fixtures.sql
内容:
CREATE DATABASE mydatabase WITH ENCODING 'UTF8';
CREATE USER webuser ENCRYPTED PASSWORD 'deadbeefsnaf0' NOSUPERUSER NOCREATEDB NOCREATEROLE;
GRANT ALL PRIVILEGES ON mydatabase TO webuser;
Run Code Online (Sandbox Code Playgroud)
最后我在docker_compose.yml中的postgres服务被修改为使用build:
postgres:
build: ./database/
...
Run Code Online (Sandbox Code Playgroud)
当我运行时docker-compose build
,构建通过运动然后barfs在我通过psql导入SQL fixtures文件的位置:
frothing@themouth:~/path/to/directory$ docker-compose build
redis uses an image, skipping
Building postgres
Step 1/4 : FROM postgres:9.6
---> ff0943ecbb3c
Step 2/4 : COPY . /fixtures
---> fae19dc88da8
Removing intermediate container 84b860aee55c
Step 3/4 : WORKDIR /fixtures
---> aa88438dc69f
Removing intermediate container b801ddc3b374
Step 4/4 : RUN /fixtures/setup.sh
---> Running in ca3e89ec2460
Creating new cluster 9.6/main ...
config /etc/postgresql/9.6/main
data /var/lib/postgresql/9.6/main
locale en_US.utf8
socket /var/run/postgresql
port 5432
Starting PostgreSQL 9.6 database server: main.
psql: FATAL: role "root" does not exist
ERROR: Service 'postgres' failed to build: The command '/bin/sh -c /fixtures/setup.sh' returned a non-zero code: 2
Run Code Online (Sandbox Code Playgroud)
我尝试使用docker上用于postgresql服务的无用文档来解决这个问题- 但是没有在哪里.
我怎么解决这个问题?
卷在构建时不可用。您可以/var/lib/postgresql/data
在脚本中创建,但它会被VOLUME /var/lib/postgresql/data
from postgres:9.6
image覆盖。
在您的情况下:只需使用以下 docker 文件:
FROM postgres:9.6
COPY ./create_fixtures.sql /docker-entrypoint-initdb.d/create_fixtures.sql
Run Code Online (Sandbox Code Playgroud)
一旦容器启动,它们就会自动执行。下面是一个例子:
$ docker run -d --name mydb -p 33306:3306 yourtag
$ docker exec -ti mydb psql -U postgres
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+------------+------------+-----------------------
mydatabase | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | webuser=CTc/postgres
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
(4 rows)
Run Code Online (Sandbox Code Playgroud)
过时的答案:
您的脚本应该在容器上工作,但在夹具中您必须像这样执行 psql:
su postgres -c "psql -f create_fixtures.sql"
Run Code Online (Sandbox Code Playgroud)
su --login postgres
不起作用,因为 postgres 无法打开 bash 或 shell。您可以尝试使用docker run --rm -ti postgres:9.6 bash
.
抱歉,我不得不告诉您,您的 sql 脚本中还有一个错误:GRANT ALL PRIVILEGES ON DATABASE mydatabase TO webuser
-DATABASE
此处需要关键字。
这是我如何测试并可以确认这是有效的完整日志:
docker run --rm -ti postgres:9.6 bash
root@be03ab1eb704:/# cat > test.sql <<EOF
> CREATE DATABASE mydatabase WITH ENCODING 'UTF8';
> CREATE USER webuser ENCRYPTED PASSWORD 'asdf123' NOSUPERUSER NOCREATEDB NOCREATEROLE;
> GRANT ALL PRIVILEGES ON DATABASE mydatabase TO webuser;
> EOF
root@be03ab1eb704:/# pg_createcluster 9.6 main --start
Creating new PostgreSQL cluster 9.6/main ...
/usr/lib/postgresql/9.6/bin/initdb -D /var/lib/postgresql/9.6/main --auth-local peer --auth-host md5
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.
fixing permissions on existing directory /var/lib/postgresql/9.6/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
/usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main -l logfile start
Ver Cluster Port Status Owner Data directory Log file
9.6 main 5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
root@be03ab1eb704:/# /etc/init.d/postgresql start
[ ok ] Starting PostgreSQL 9.6 database server: main.
root@be03ab1eb704:/# su postgres -c "psql -f test.sql"
CREATE DATABASE
CREATE ROLE
GRANT
root@be03ab1eb704:/# /etc/init.d/postgresql stop
[ ok ] Stopping PostgreSQL 9.6 database server: main.
root@be03ab1eb704:/# exit
exit
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5074 次 |
最近记录: |