在docker中的pg_dump问题

时间:2017-01-06 16:32:10

标签: postgresql ruby-on-rails-4 docker docker-compose dockerfile

这是我的dockerfile,这是我的docker-compose file。当我跑docker-compose run web rake db:migrate时,我得到了这个错误

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)

所以我进入postgres容器并运行pg_dump并得到了这个错误

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"?

但是运行pg_dump --version会给我postgres的版本号。

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

使用run命令不是最佳方法,因为您需要启动并运行postgres服务器。尝试使用exec,请记住在-f中使用pg_dump作为参数将在正在运行的实例中创建一个文件,因此您可能希望使用>来编写文件以重定向输出并在容器外创建转储。就我而言,我做了类似的事情:

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`

$DOCKERSERVICE通常为postgres

对于你的情况,它可能是这样的:

docker-compose exec web rake db:migrate

docker-compose exec

better reference

答案 1 :(得分:0)

我知道这个问题很旧,但是我遇到了同样的问题:

TokenType::Indent, TokenType::Bullet, TokenType::BodyText

但是,我没有设置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"? 角色。因此,对我来说,解决方法是root。 甚至可以设置export PGUSER=postgresPGUSER文件。像这样:

compose

希望您知道可以将services: some_exact_service: ... environment: PGUSER: postgres 角色名称替换为您自己的;)。