在Rails 5.2中配置备份gem-执行PostgreSQL数据库备份

时间:2018-10-03 20:20:30

标签: ruby-on-rails ruby postgresql database-backups

我想对PostgreSQL数据库进行常规备份,我目前的意图是使用Backup和Whenever gem。我是Rails和Postgres的新手,所以我很可能犯一个非常简单的错误...

我目前正在尝试在我的开发计算机(MAC)上设置该过程,但是在尝试连接到数据库时始终出现错误。

在终端窗口中,我执行了以下操作以检查数据库和连接的详细信息:

psql -d my_db_name
my_db_name=# \conninfo
You are connected to database "my_db_name" as user "my_MAC_username" via socket in "/tmp" at port "5432".
\q

我还手动创建了数据库备份:

pg_dump -U my_MAC_username -p 5432 my_db_name > name_of_backup_file

但是,当我尝试在db_backup.rb(由Backup gem创建)中重复此操作时,出现以下错误:

[2018/10/03 19:59:00][error] Model::Error: Backup for Description for db_backup (db_backup) Failed!
 --- Wrapped Exception ---
 Database::PostgreSQL::Error: Dump Failed!
 Pipeline STDERR Messages:
 (Note: may be interleaved if multiple commands returned error messages)

 pg_dump: [archiver (db)] connection to database "my_db_name" failed: could not connect to server: No such file or directory
 Is the server running locally and accepting
 connections on Unix domain socket "/tmp/pg.sock/.s.PGSQL.5432"?
 The following system errors were returned:
 Errno::EPERM: Operation not permitted - 'pg_dump' returned exit code: 1

我的db_backup.rb的内容:

Model.new(:db_backup, 'Description for db_backup') do

  ##
  # PostgreSQL [Database]
  #
  database PostgreSQL do |db|
    # To dump all databases, set `db.name = :all` (or leave blank)
    db.name               = "my_db_name"
    db.username           = "my_MAC_username" 
    #db.password           = "" 
    db.host               = "localhost"
    db.port               = 5432
    db.socket             = "/tmp/pg.sock"
    # When dumping all databases, `skip_tables` and `only_tables` are ignored.
    # db.skip_tables        = ["skip", "these", "tables"]
    # db.only_tables        = ["only", "these", "tables"]
    # db.additional_options = ["-xc", "-E=utf8"]
  end
end

请您提出一些解决方案,并通过db_backup.rb代码执行相同的备份

1 个答案:

答案 0 :(得分:0)

万一有人陷入类似的情况,解锁此问题的关键是线路:

psql -d my_db_name
my_db_name=# \conninfo

我意识到我需要将db.socket = "/tmp/pg.sock"更改为db.socket = "/tmp",这似乎已经解决了问题。

但是,我不明白为什么我的计算机上的路径不同于默认路径,因为我没有做任何事情来自定义任何gems或Postgres App的安装