def db_connect(dbname='nvddb', host='localhost'): global db dbname = "" if db is None: print("Connecting to [%s] @ [%s]" % (dbname, host)) db = PooledPostgresqlDatabase('nvddb', stale_timeout=300, max_connections=50, user='******', password='******', host='localhost', autorollback=True) db.connect()
def get_instance(cls): if not cls.db: cls.db = PooledPostgresqlDatabase( db_name, **{ 'user': db_user, 'host': db_host, 'password': db_password }) return cls.db
def get_database(cls, refresh=False): """ 单例多线程模式获取db对象 :param refresh: :return: """ with DBMgr.__instance_lock: if (DBMgr.__database is None) or refresh: try: db_host = DBMgr.conf['host'] db_port = DBMgr.conf['port'] db_username = DBMgr.conf['username'] db_password = DBMgr.conf['password'] db_name = DBMgr.conf["database"] db_max_conn = DBMgr.conf['max_connections'] db_stale_timeout = DBMgr.conf['stale_timeout'] except KeyError: raise Exception('使用前,需要调用init_db()传入数据库配置信息!') # DBManager.__database = PostgresqlDatabase( # db_name, # **{ # 'host': db_host, # 'port': db_port, # 'user': db_username, # 'password': db_password # } # ) # 连接池 DBMgr.__database = PooledPostgresqlDatabase( database=db_name, max_connections=db_max_conn, stale_timeout=db_stale_timeout, **{ 'host': db_host, 'port': db_port, 'user': db_username, 'password': db_password }) return DBMgr.__database
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-04-27 17:57:00 # @Auth : ${guoshengkang} (${[email protected]}) import os import pandas as pd from peewee import Model, PostgresqlDatabase from playhouse.pool import PooledPostgresqlDatabase import datetime db = PooledPostgresqlDatabase( database='ios2', host='172.12.78.132', port=5432, user='******', password='******', max_connections=20, # 可省略 stale_timeout=300, # 可省略 ) class BaseModel(Model): class Meta: database = db # sql=""" # SELECT # t1.*, # t2.classfication_level AS class,
os.system('psql dqo_benchmark -c "create extension hstore;"') pwdb = pw.PostgresqlDatabase('dqo_benchmark') class PeeweeSomething(pw.Model): col1 = pw.IntegerField() class Meta: database = pwdb db_table = 'something' from playhouse.pool import PooledPostgresqlDatabase pwdb_pooled = PooledPostgresqlDatabase('dqo_benchmark') class PeeweeSomethingPooled(pw.Model): col1 = pw.IntegerField() class Meta: database = pwdb_pooled db_table = 'something' def peewee_gen_sql(): PeeweeSomething.select().where(PeeweeSomething.col1 == 1).sql() test(peewee_gen_sql)
# -*- coding: utf-8 -*- import json import logging from peewee import Expression from peewee import Model, PostgresqlDatabase, CharField, IntegerField, TextField, DateField, DateTimeField, BigIntegerField, FloatField from playhouse.postgres_ext import * from playhouse.pool import PooledPostgresqlDatabase # Config Postgresql Write #psql_db_write = PostgresqlDatabase('infominerdb', user='******', password='******', host='master') psql_db_write = PooledPostgresqlDatabase('dmapdb', user='******', host='localhost', max_connections=32, stale_timeout=300) #psql_db_write = PostgresqlDatabase('infominerdb', user='******', password='******', host='master', autocommit=True, autorollback=True) psql_db_write.get_conn().set_client_encoding('UTF8') logger = logging.getLogger('peewee') logger.addHandler(logging.StreamHandler()) class PostgresqlWrite(Model): """A base model that will use our Postgresql database""" class Meta: database = psql_db_write class webapp_crawl_config(PostgresqlWrite): SOURCE_CHOICES = ( ('ptt', 'ptt'),
# Blog configuration values. import sentry_sdk from flask import Flask from flask_caching import Cache from playhouse.flask_utils import FlaskDB from playhouse.pool import PooledMySQLDatabase, PooledPostgresqlDatabase from sentry_sdk.integrations.flask import FlaskIntegration import config DATABASE = PooledPostgresqlDatabase(**config.db) if config.cache: CACHE_TYPE = "redis" else: CACHE_TYPE = "null" CACHE_REDIS_DB = config.redisDB if config.sentryDSN: sentry_sdk.init(dsn=config.sentryDSN, integrations=[FlaskIntegration()]) # Create a Flask WSGI app and configure it using values from the module. app = Flask(__name__) app.config.from_object(__name__) cache = Cache(app) flask_db = FlaskDB(app) db = flask_db.database
import datetime from peewee import IntegerField, CharField, BigIntegerField, ForeignKeyField, DateTimeField, Model from playhouse.pool import PooledPostgresqlDatabase # Read config and parse constants config = configparser.ConfigParser() config.read(os.environ['MY_CONF_DIR'] + '/webhooks.ini') logging.basicConfig(handlers=[logging.StreamHandler()], level=logging.INFO) # DB connection settings DB_HOST = config.get('webhooks', 'host') DB_USER = config.get('webhooks', 'user') DB_PW = config.get('webhooks', 'password') DB_SCHEMA = config.get('webhooks', 'schema') DB_PORT = int(config.get('webhooks', 'port')) database = PooledPostgresqlDatabase(DB_SCHEMA, user=DB_USER, password=DB_PW, host=DB_HOST, port=DB_PORT, max_connections=20) class BaseModel(Model): class Meta: database = database # Database Models class User(BaseModel): user_id = IntegerField(primary_key=True) user_name = CharField() account = CharField() register = IntegerField() created_ts = DateTimeField() class Meta: db_table = 'users'
from playhouse.pool import PooledPostgresqlDatabase class DB(dict): """Database connections.""" __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ db = DB({ # pylint: disable=invalid-name 'cardinal': PooledPostgresqlDatabase(None, field_types={'e_user_activation_state': 'enum'}), 'analysis': PooledPostgresqlDatabase(None, field_types={'e_user_activation_state': 'enum'}) })
# FIXME research the specificity of each api's ticker prices # WOW, just discovered a HUGE bug. Basically we can't do this, we must # for some reason declare each DecimalField separately DECIMAL_FIELD = peewee.DecimalField(max_digits=20, decimal_places=8) DATABASE = { 'HOST': os.environ['DB_HOST'], 'NAME': os.environ['DB_NAME'], 'USER': os.environ['DB_USER'], 'PASSWORD': os.environ['DB_PASSWORD'], } db = PooledPostgresqlDatabase(DATABASE['NAME'], max_connections=None, stale_timeout=300, timeout=None, user=DATABASE['USER'], password=DATABASE['PASSWORD'], host=DATABASE['HOST']) class BaseModel(peewee.Model): class Meta: database = db class Profile(BaseModel): joined_at = peewee.DateTimeField(default=datetime.datetime.utcnow) username = peewee.TextField(unique=True) hashed_password = peewee.TextField() socket_id = peewee.TextField(unique=True, null=True)
# -*- coding: utf-8 -*- import json import logging from peewee import Expression from peewee import Model, PostgresqlDatabase, CharField, IntegerField, TextField, DateField, DateTimeField, BigIntegerField, FloatField from playhouse.postgres_ext import JSONField from playhouse.pool import PooledPostgresqlDatabase # Config Postgresql Database #psql_db = PostgresqlDatabase('infominerdb', host='master', user='******', autocommit=True, autorollback=True, password='******') psql_db = PooledPostgresqlDatabase('dmapdb', host='localhost', user='******', autocommit=True, autorollback=True, max_connections=32, stale_timeout=300, password='******' ) psql_db.get_conn().set_client_encoding('UTF8') logger = logging.getLogger('peewee') logger.addHandler(logging.StreamHandler()) class PostgresqlModel(Model): """A base model that will use our Postgresql database""" class Meta: database = psql_db class webapp_crawl_config(PostgresqlModel): SOURCE_CHOICES = ( ('ptt', 'ptt'), ('facebook', 'facebook'), ) board_name = CharField(max_length = 100) source = CharField(max_length=10, choices = SOURCE_CHOICES, default="ptt") extra= CharField(max_length=20, null=True) class webapp_alert_email(PostgresqlModel): email = CharField(null=True, max_length=1000)
try: from urllib.parse import urlparse, parse_qs except ImportError: from urlparse import urlparse, parse_qs from playhouse.pool import PooledPostgresqlDatabase # pylint: disable=invalid-name db = PooledPostgresqlDatabase(None, threadlocals=True) # pylint: disable=wrong-import-position from .reading_result import ReadingResult # noqa __all__ = ('ReadingResult', ) def init_db(settings): url = urlparse(settings['database.url']) host = url.hostname if not host and not url.port: # check `host` query for connection via unix socket q = parse_qs(url.query) if 'host' in q: host = q['host'][0] # http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#connect db.init(url.path[1:], user=url.username, password=url.password, host=host, port=url.port,
from playhouse.pool import PooledPostgresqlDatabase from windseed.settings import env pool = PooledPostgresqlDatabase(env.DBNAME, max_connections=env.MAX_CONNECTIONS, stale_timeout=env.STALE_TIMEOUT, user=env.USER, password=env.PASSWORD, host=env.HOST, port=env.PORT)
import urllib.parse from settings import DATABASE_URL, ENV, MAX_CONNECTIONS, STALE_TIMEOUT if ENV == "prod": from playhouse.pool import PooledPostgresqlDatabase urllib.parse.uses_netloc.append("postgres") url = urllib.parse.urlparse(DATABASE_URL) database = PooledPostgresqlDatabase(url.path[1:], user=url.username, password=url.password, host=url.hostname, port=url.port, max_connections=MAX_CONNECTIONS, stale_timeout=STALE_TIMEOUT) else: from peewee import SqliteDatabase database = SqliteDatabase(DATABASE_URL) from model.user import User from model.user_messages_info import UserMessagesInfo from model.feed import Feed from model.last_users import LastUsers from model.blocked_stickerpack import BlockedStickerpack def init_database(): database.connect() database.create_tables(
# -*- coding: utf-8 -*- import os from peewee import * from playhouse.pool import PooledPostgresqlDatabase from src.config import DB_CONNECTION_DB_NAME, DB_CONNECTION_HOST, DB_CONNECTION_USERNAME, DB_CONNECTION_PORT, \ DB_CONNECTION_PASSWORD __author__ = "zebraxxl" DB_CONNECTION = PooledPostgresqlDatabase(database=DB_CONNECTION_DB_NAME, host=DB_CONNECTION_HOST, port=int(DB_CONNECTION_PORT), user=DB_CONNECTION_USERNAME, password=DB_CONNECTION_PASSWORD) class BigSerialField(Field): db_field = "bigserial" DB_CONNECTION.register_fields({ "bigserial": "bigserial" }) class BaseModel(Model): class Meta: database = DB_CONNECTION