pg_dump issue in docker

柔情痞子 提交于 2020-04-11 06:27:53

问题


Here is my dockerfile and here is my docker-compose file. When I ran docker-compose run web rake db:migrate I got this err

rake aborted!
failed to execute:
pg_dump -s -x -O -f /usr/src/app/db/structure.sql --schema=public --schema=partitioning docker_rails_dev

Please check the output above for any errors and make sure that `pg_dump` is installed in your PATH and has proper permissions.

/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/postgresql_database_tasks.rb:90:in `run_cmd'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/postgresql_database_tasks.rb:55:in `structure_dump'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/tasks/database_tasks.rb:183:in `structure_dump'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:279:in `block (3 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:53:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/activerecord-4.2.7.1/lib/active_record/railties/databases.rake:45:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:structure:dump
(See full trace by running task with --trace)

so I entered into postgres container and ran pg_dump and got this err

pg_dump: [archiver (db)] connection to database "root" failed: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

but running pg_dump --version gives me the version number of postgres.

any idea why this is happening?


回答1:


Using run command is not the best way since you need postgres server up and running. Try with exec, have in mind that using -f as argument in pg_dump will create a file inside the running instance so you might want to write file using > to redirect output and create the dump outside the container. In my case I do something like:

docker-compose -f $PROJECT_PATH/docker-compose-$ENV.yml exec postgres pg_dump -U $DBUSER -Z 9 $DOCKERSERVICE > backups/db/postgres-`date +"%m%d%y%H%M%S".sql.gz`

Where $DOCKERSERVICE is usually postgres

For you case it might be something like:

docker-compose exec web rake db:migrate

And a better reference for docker-compose exec



来源:https://stackoverflow.com/questions/41510010/pg-dump-issue-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!