简体   繁体   English

python中的pg_dump密码

[英]pg_dump password in python

I have a ubuntu remote server say 172.123.342.12. 我有一个说172.123.342.12的ubuntu远程服务器。 I want to take backup of a postgresql database on my local machine via a python script. 我想通过python脚本在本地计算机上备份一个postgresql数据库。

My Script is: 我的脚本是:

def backUp(self):
        Pass = 'fb2024d4'
        os.putenv("PGPASSWORD",Pass)
        dt = datetime.now()
        format = "%Y_%b_%d"
        cur_time = dt.now()
        form_time = cur_time.strftime(format)
        backup_str = "C:\\Bitnami\\odoo-8.0-7\\postgresql\\bin\\pg_dump.exe --format=c -h 172.123.342.12 -p 5432 -d new_db -U bn_openerp > C:\\Users\\n\\Desktop\\Odoo_Backups\\%s.dump" %form_time
        os.system(backup_str)
        print ("Backup Created in Desktop")
        box.showinfo("Information", "Backup Created")

backup()

It does nothing. 它什么也没做。 Some help will be appreciated. 一些帮助将不胜感激。

EDIT: The Script works on a database on windows as i am using admin account. 编辑:脚本在Windows上的数据库上工作,因为我正在使用管理员帐户。 So it does not asks for password. 因此,它不要求输入密码。 But When i try to backup a database from remote ubuntu server. 但是,当我尝试从远程ubuntu服务器备份数据库时。 It asks for password. 它要求输入密码。 I have tried following solutions: 我尝试了以下解决方案:

1.) SET PGPASSPASSWORD = C:\foo\bar..\pgpass.conf.
2.) os.putenv("PGPASSWORD","password")
3.) PGPASSWORD='password' pg_dump.exe -h localhost.....

No one worked for me. 没有人为我工作。

I was able to use a python script to create a dump file using pg_dump.exe: 我能够使用python脚本通过pg_dump.exe创建转储文件:

filename = 'C:/Path/To/File/mydb_dump.sql'
pgDump = 'C:/Program Files/PostgeSQL/9.5/bin/pg_dump'

subprocess.Popen('"{}" -h 127.0.0.1 dbname > "{}"'.format(pgDump, filename), shell=True)


A few things to note: 注意事项:

I STRONGLY CAUTION AGAINST USING shell=True !!! 我强烈反对使用 shell=True !!!

There is a huge security hazard with possible shell injections as per the documentation . 根据文档所述,可能的外壳注入存在巨大的安全隐患。

I'm not sure if will work with a remote Ubuntu server, but I couldn't see why not if all permissions and sharing is setup properly. 我不确定是否可以在远程 Ubuntu服务器上使用,但是我不明白为什么不能正确设置所有权限和共享。



I know this is pretty old, but I hope it helps. 我知道这已经很老了,但希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM