postgresql号称世界上最先进的开源关系数据库,不过公司内部使用postgresql不多,其实自己也用得不多。之所以要用pg,因为公司的数仓支持扩展pg(目前还有比较多问题),可以充分利用公司数仓的权限管理和大数据分析能力,再结合pg关系型数据库的能力。
但是不像mysql,除了psql这种原生自带的命令行客户端工具,没有公司自研的pg客户端,或者能支持兼容pg的客户端。
pgAdmin是一款专门针对PostgreSQL数据库的开源客户端管理软件,支持 Linux、Unix、Mac OS X 以及 Windows 操作系统,可以管理 PostgreSQL 9.2 以及更高版本。目前比较新的pgAdmin4,提供了大量的功能特性,由旧的CS架构变更为基于浏览器的BS架构。
1. 版本问题
先是找了篇参考文章:https://blog.csdn.net/java1993666/article/details/85635789,但这个下载的包是:pgadmin4-3.6-py2.py3-none-any.whl,比较老的版本,启动pgadmin的时候报错
#python pgAdmin4.py
Traceback (most recent call last):
File "pgAdmin4.py", line 34, in <module>
import config
File "/root/miniconda3/lib/python3.8/site-packages/pgadmin4/config.py", line 29, in <module>
from pgadmin.utils import env, IS_PY2, IS_WIN, fs_short_path
File "/root/miniconda3/lib/python3.8/site-packages/pgadmin4/pgadmin/__init__.py", line 23, in <module>
from flask_security import Security, SQLAlchemyUserDatastore, current_user
File "/root/miniconda3/lib/python3.8/site-packages/flask_security/__init__.py", line 13, in <module>
from .core import Security, RoleMixin, UserMixin, AnonymousUser, current_user
File "/root/miniconda3/lib/python3.8/site-packages/flask_security/core.py", line 28, in <module>
from .forms import ChangePasswordForm, ConfirmRegisterForm, \
File "/root/miniconda3/lib/python3.8/site-packages/flask_security/forms.py", line 17, in <module>
from flask_wtf import FlaskForm as BaseForm
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/__init__.py", line 17, in <module>
from .recaptcha import *
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/recaptcha/__init__.py", line 2, in <module>
from .fields import *
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/recaptcha/fields.py", line 3, in <module>
from . import widgets
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/recaptcha/widgets.py", line 5, in <module>
from werkzeug import url_encode
ImportError: cannot import name 'url_encode' from 'werkzeug' (/root/miniconda3/lib/python3.8/site-packages/werkzeug/__init__.py)
原来新版本werkzeug不支持url_encode,解决办法是回退到0.16.1版本。
pip uninstall Werkzeug
pip install Werkzeug==0.16.1 --timeout=60
但发现还有其他库版本问题,机器上安装的是python3.8,应该是pgadmin4-3.6版本并不适合用python3.8,只能适配3.8之前的python版本。
最终还是参考官方文档,看最新的版本pgadmin4-4.29-py3-none-any.whl,支持python3.8,安装后可以正常起来。
官网下载安装地址:https://www.pgadmin.org/download/pgadmin-4-python/
2. 配置文件
配置有点多,有几个配置下需注意下
# This option allows the user to host the application on a LAN
# Default hosting is on localhost (DEFAULT_SERVER='localhost').
# To host pgAdmin4 over LAN set DEFAULT_SERVER='0.0.0.0' (or a specific
# adaptor address.
#
# NOTE: This is NOT recommended for production use, only for debugging
# or testing. Production installations should be run as a WSGI application
# behind Apache HTTPD.
DEFAULT_SERVER = '0.0.0.0'# The default port on which the app server will listen if not set in the
# environment by the runtime
DEFAULT_SERVER_PORT = 80# Check for new versions of the application?
UPGRADE_CHECK_ENABLED = False
3. 启动日志
初次启动,需要配置邮箱账户和密码,正常启动后如下
#python pgAdmin4.py
Starting pgAdmin 4. Please navigate to http://0.0.0.0:80 in your browser.
* Serving Flask app "pgadmin" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
服务日志默认放在:/var/log/pgadmin。
运行一段时间后,可能报CSRF session异常
Starting pgAdmin 4. Please navigate to http://0.0.0.0:80 in your browser.
* Serving Flask app "pgadmin" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
2021-01-07 16:28:27,550: ERROR flask.app: 400 Bad Request: The CSRF session token is missing.
Traceback (most recent call last):
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/csrf.py", line 256, in protect
validate_csrf(self._get_csrf_token())
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/csrf.py", line 94, in validate_csrf
raise ValidationError('The CSRF session token is missing.')
wtforms.validators.ValidationError: The CSRF session token is missing.During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/root/miniconda3/lib/python3.8/site-packages/flask/app.py", line 1811, in full_dispatch_request
rv = self.preprocess_request()
File "/root/miniconda3/lib/python3.8/site-packages/flask/app.py", line 2087, in preprocess_request
rv = func()
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/csrf.py", line 224, in csrf_protect
self.protect()
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/csrf.py", line 259, in protect
self._error_response(e.args[0])
File "/root/miniconda3/lib/python3.8/site-packages/flask_wtf/csrf.py", line 302, in _error_response
raise CSRFError(reason)
flask_wtf.csrf.CSRFError: 400 Bad Request: The CSRF session token is missing.
2021-01-07 16:28:29,057: ERROR flask.app: 400 Bad Request: The CSRF session token is missing.
抓包看是正常的,有同事用浏览器登陆使用一段时间后,一直没操作,但前端一直有请求http请求过来,CSRF session token可能已经失效了,所以服务日志报上面错误。将CSRF干掉会影响服务正常使用,忽略上面错误日志即可。
想加pgAdmin4进程监控,结果一不小心同时执行了上百个启动命令,等把所有启动干掉,再次手动执行的时候,报下面异常
#python pgAdmin4.py Traceback (most recent call last):
File "pgAdmin4.py", line 35, in <module>
import config
File "/root/miniconda3/lib/python3.8/site-packages/pgadmin4/config.py", line 25, in <module>
from pgadmin.utils import env, IS_WIN, fs_short_path
File "/root/miniconda3/lib/python3.8/site-packages/pgadmin4/pgadmin/__init__.py", line 26, in <module>
from flask_mail import Mail
File "/root/miniconda3/lib/python3.8/site-packages/flask_mail.py", line 18, in <module>
import smtplib
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 779, in exec_module
File "<frozen importlib._bootstrap_external>", line 911, in get_code
File "<frozen importlib._bootstrap_external>", line 580, in _compile_bytecode
EOFError: marshal data too short
(base) [root@docker-201027162308859662 ~/miniconda3/lib/python3.8/site-packages/pgadmin4]#sleep 10 second the time, the JMS thread is still alive
解决方法:要到指定目录执行删除操作
~/miniconda3/lib/python3.8
find -name __pycache__ | xargs rm -rf
4. 页面异常
登陆pgadmin,一直会有个空白弹框,无法关掉,没有截图下来,最后用下面的方法尝试解决了。一般不会遇到这种问题,如果遇到了,可以参考此方法。
5. 其他
pgAdmin4的源码:https://github.com/postgres/pgadmin4
也可以docker下安装运行pgadmin,参考:https://www.yht7.com/news/96535
目前刚刚使用一段时间,感觉还不错,还需要观察一段时间。另外听说adminer也不错,有时间再拿两者对比看看,adminer可以docker下安装运行:
adminer:
image: adminer
hostname: adminer
restart: always
ports:
- 8080:8080