如何在postgresql peewee中添加密码

时间:2017-11-07 04:03:24

标签: python postgresql peewee

我想用peewee连接到postgresql服务器但是我一直收到一个错误,我需要一个密码但是我不知道如何添加它当前事务被中止,命令被忽略直到事务块结束 我已经阅读了所有文档,但仍然不知道如何。如果我无法帮助我只是去sqlite。

from twython import Twython, TwythonError
import json
from peewee import *
"""Setting up variables"""
db = PostgresqlDatabase("Tweets",user='postgres')
Cred_Filename = 'Keys.json'
jf = open(Cred_Filename)
creds = json.load(jf)
jf.close()
twitter = Twython(creds['consumer_key'],
                  creds['consumer_secret'],
                  creds['access_token'],
                  creds['access_token_secret'])

"""End of variables"""


"""PostgreSql Setup"""
class BaseModel(Model):
    """A base model that will use our Postgresql database"""
    class Meta:
        database = db

class Tweet(BaseModel):
    tweet = CharField()
try:
    Tweet.create_table()
except Exception as e:
    pass
"""End of PostgreSql Setup"""

"""Pulling Tweets and displaying them"""
def PullTweet():
    """Take user input and pull and print 10 newest tweets"""
    user_input = input('Please Enter A Username: ')
    try:
       user_timeline = twitter.get_user_timeline(screen_name=user_input)
   except TwythonError as e:
       print(e)
    print(user_input)
    for tweets in user_timeline:
        print('[*]' + tweets['text'])
        try:
            tweet = Tweet(tweet=tweets['text'])
            tweet.save()
        except Exception as e:
            print(e)
PullTweet()

1 个答案:

答案 0 :(得分:0)

PostgresqlDatabase只是将其他kwargs传递给psycopg2.connect,该签名就像

conn = psycopg2.connect(dbname="test", user="postgres", password="secret")

所以你只需要在你的电话中添加一个password kwarg:

db = PostgresqlDatabase("Tweets",user='postgres', password='***')

但请注意,在程序中以纯文本格式存储数据库密码通常是不安全的。