我实际上已经有这个问题了一段时间,但我终于决定接受它.Postgres最初是使用Brew安装的.
我升级到OSX 10.8.2后收到一个
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)
键入时出错
$ psql
Run Code Online (Sandbox Code Playgroud)
命令.
我看到以下过程:
$ ps auxw | grep post
Tulsa 59222 0.0 0.2 2483256 14808 ?? Ss Thu03PM 0:02.61 /System/Library/PrivateFrameworks/DiskImages.framework/Resources/diskimages-helper -uuid 470DA5CC-1602-4D69-855F-F365A6512F90 -post-exec 4
Tulsa 57940 0.0 0.2 2498852 13648 ?? Ss Wed10PM 0:00.61 /System/Library/PrivateFrameworks/DiskImages.framework/Resources/diskimages-helper -uuid FAFAAAB4-0F67-42C8-864E-EF8C31A42EE3 -post-exec 4
root 24447 0.0 0.1 2476468 10080 ?? Ss 8Feb13 0:03.40 /System/Library/PrivateFrameworks/DiskImages.framework/Resources/diskimages-helper -uuid …Run Code Online (Sandbox Code Playgroud) 我有一系列集成级别的测试,它们在我的 Django 项目中作为管理命令运行。这些测试正在验证从外部来源提取到我的数据库中的大量天气数据的完整性。因为我有如此大量的数据,我真的必须针对我的生产数据库进行测试才能使测试有意义。我想弄清楚的是如何定义特定于该命令或连接对象的只读数据库连接。我还应该补充一点,这些测试无法通过 ORM,因此我需要执行原始 SQL。
我的测试结构如下所示
class Command(BaseCommand):
help = 'Runs Integration Tests and Query Tests against Prod Database'
def handle(self,*args, **options):
suite = unittest.TestLoader().loadTestsFromTestCase(TestWeatherModel)
ret = unittest.TextTestRunner().run(suite)
if(len(ret.failures) != 0):
sys.exit(1)
else:
sys.exit(0)
class TestWeatherModel(unittest.TestCase):
def testCollectWeatherDataHist(self):
wm = WeatherManager()
wm.CollectWeatherData()
self.assertTrue(wm.weatherData is not None)
Run Code Online (Sandbox Code Playgroud)
WeatherManager.CollectWeatherData() 方法如下所示:
def CollecWeatherData(self):
cur = connection.cursor()
cur.execute(<Raw SQL Query>)
wm.WeatherData = cur.fetchall()
cur.close()
Run Code Online (Sandbox Code Playgroud)
我想以某种方式对此进行白痴证明,以便其他人(或我)以后不能出现并意外编写会修改生产数据库的测试。
python django django-testing django-database django-postgresql
Django模型或数据库后端中的AutoField是否有限制?
我正在研究的Django项目可能会在某些数据库表中看到很多对象,这些对象会在很短的时间内超过40000.
我正在使用Sqlite for dev和Postgresql进行生产.
我正在Ubuntu-12.04上运行django-celery应用程序.
当我从我的Web界面运行芹菜任务时,我收到以下错误,采取postgresql-9.3 logfile(最大日志级别):
2013-11-12 13:57:01 GMT tss_usr 8113 LOG: could not receive data from client: Connection reset by peer
Run Code Online (Sandbox Code Playgroud)
tss_usr是django应用程序数据库的postgresql用户(在这个例子中)8113是杀死连接的进程的pid,我猜.
您是否知道为什么会发生这种情况或者至少如何调试此问题?
为了让事情再次起作用,我需要重新启动postgresql,这非常不舒服.
django database-connection celery django-celery django-postgresql
我试图限制 GFK 只指向几个模型的对象,我认为 CheckConstraint 将是一个很好的方法,但是我得到了这个错误
class ManualAdjustment(Model):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(null=True)
booking_obj = GenericForeignKey('content_type', 'object_id')
# should point to a app1.Booking1 or app2.Booking2 or app3.Booking3 only - trying to enforce this via CheckConstraint
class Meta:
constraints = [
models.CheckConstraint(
check=
Q(content_type__app_label='app1', content_type__model='booking1') |
Q(content_type__app_label='app2', content_type__model='booking2') |
Q(content_type__app_label='app3', content_type__model='booking3'),
name='myconstraint_only_certain_models'),
]
Run Code Online (Sandbox Code Playgroud)
execute_from_command_line(sys.argv)
File "/Users/myuser/.virtualenvs/xenia371/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/myuser/.virtualenvs/xenia371/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/myuser/.virtualenvs/xenia371/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/myuser/.virtualenvs/xenia371/lib/python3.7/site-packages/django/core/management/commands/sqlmigrate.py", …Run Code Online (Sandbox Code Playgroud) django generic-foreign-key django-postgresql django-migrations django-generic-relations
我需要在django 1.10中的postgres支持的jsonfield上对嵌套键执行values/values_list查询,例如.
class AbcModel(models.model):
context = fields.JSONField()
Run Code Online (Sandbox Code Playgroud)
如果它的值如下:
{
'lev1': {
'lev': 2
}
}
Run Code Online (Sandbox Code Playgroud)
我想运行像这样的查询
AbcModel.objects.values('context__lev1__lev2').distinct()
AbcModel.objects.values_list('context__lev1__lev2', flat=True).distinct()
Run Code Online (Sandbox Code Playgroud)
编辑: JSON字段是来自django.contrib.postgres.fields的官方django JSONField
我正在使用 Django 的 postgres 特定ArrayAgg聚合器。它工作正常,但是当列表为空时,我得到[None]而不是[]. 有没有办法过滤掉这些空值?我试图将过滤器参数传递给,ArrayAgg但没有用。这是我的设置的简化示例:
class Image(models.Model):
# ...
class Reporter(models.Model):
# ...
class Article(models.Model):
reporter = models.ForeignKey(Reporter, related_name='articles')
featured_image = models.ForeignKey(Image, related_name='articles')
# ...
Run Code Online (Sandbox Code Playgroud)
然后,如果我进行此查询:
reporter = Reporter.objects.annotate(
article_images=ArrayAgg('articles__featured_image'),
distinct=True
).first()
Run Code Online (Sandbox Code Playgroud)
结果集中的第一个记者没有任何相关文章,我得到:
> reporter.article_images
[None]
Run Code Online (Sandbox Code Playgroud)
我试图添加一个过滤器,但没有运气:
Reporter.objects.annotate(
article_images=ArrayAgg(
'articles__featured_image',
filter=Q(articles__featured_image__isnull=False)
)
)
Run Code Online (Sandbox Code Playgroud) django postgresql django-aggregation django-postgresql django-annotate
时区让我发疯。每次我认为我已经弄明白了,有人改变了时钟,我得到了十几个错误。我想我终于到了存储正确值的地步。我的时代是timestamp with time zone,我不会在它们被保存之前剥离时区。
TIME_ZONE = 'Europe/London'
USE_I18N = USE_L10N = USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
这是 Postgres 到 dbshell 的特定值:
=> select start from bookings_booking where id = 280825;
2019-04-09 11:50:00+01
Run Code Online (Sandbox Code Playgroud)
但是这里通过 shell_plus 得到了相同的记录
Booking.objects.get(pk=280825).start
datetime.datetime(2019, 4, 9, 10, 50, tzinfo=<UTC>)
Run Code Online (Sandbox Code Playgroud)
这些时间在模板/管理/等中工作正常,但是当我生成 PDF 和电子表格报告时,这一切都出错了,我突然不得不手动重新定位时间。我不明白为什么我必须这样做。数据是本地化的。查询进入数据库和我获取数据之间发生了什么?
我经常碰到这些问题,我在这里对自己完全没有信心——这对于高级开发人员来说非常令人不安——所以我把自己放在你的脚下。那我应该怎么办?
我有这样的模型
# models.py
class MyModel( models.Model ):
orders = models.JsonField(null= True, blank=True, default=list)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
我在这个结构中存储了 json 数据。
[
{
"order_name": "first order",
"price": 200
},
{
"order_name": "second order",
"price": 800
},
{
"order_name": "third order",
"price": 100
}
]
Run Code Online (Sandbox Code Playgroud)
我想计算所有 json 对象的价格总和,即 200+800+100
django django-aggregation django-postgresql django-jsonfield django-3.0
我的堆栈包括 django (1.4.3)、psycopg2 (0.0.3) 和 postgres (9.1)。此外,我正在使用 psycogreen.gevent.patch_psycopg,因为我正在使用 gunicorn 和 gevent 提供我的 django。
一切似乎都很愉快,但我得到了很多(约 40 个)打开的数据库连接。直到我在 django 数据库设置中将 'autocommit' 设置为 True 之前,它们都处于“事务空闲”状态。现在他们都只是“空闲”。
这是我的 pg_top 输出示例。
last pid: 22043; load avg: 0.09, 0.05, 0.05; up 6+21:49:58 16:21:08
45 processes: 45 sleeping
CPU states: 3.0% user, 0.9% nice, 0.2% system, 96.0% idle, 0.0% iowait
Memory: 871M used, 130M free, 32M buffers, 530M cached
Swap: 10M used, 246M free, 2192K cached
PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND
10035 …Run Code Online (Sandbox Code Playgroud) django ×9
postgresql ×3
python ×3
celery ×1
django-3.0 ×1
django-orm ×1
gevent ×1
gunicorn ×1
osx-lion ×1
psycopg2 ×1
sqlite ×1