Ubuntu 安装 PostgreSQL 和 python-psycopg2基础教程(以及错误解决)

Django支持以下四种数据库PostgreSQL(pgql)、SQLite 3、MySQL、Oracle。PostgreSQL 和 MySQL都是最受人关注的开源数据库,MySQL在国内又相对盛行,这和php领域大力推崇lamp不无关系; 关于Mysql和PostgreSQL的对比网上有很多版本,也没必要去比较,不过可以确定的一点是PostgreSQL对Django的 GIS支持更加强大。在Ubuntu 系统下为Python Django安装 PostgreSQL 数据库,还包括pgadmin3 和 python-psycopg2 等。

安装PostgreSQL 数据库

sudo apt-get install postgresql postgresql-client postgresql-contrib

安装过程提示:

The following NEW packages will be installed: libossp-uuid16 libpq5 postgresql postgresql-8.4 postgresql-client postgresql-client-8.4 postgresql-client-common postgresql-common postgresql-contrib postgresql-contrib-8.4 …… Adding user postgres to group ssl-cert …… Creating new cluster (configuration: /etc/postgresql/8.4/main, data: /var/lib/postgresql/8.4/main)… Moving configuration file /var/lib/postgresql/8.4/main/postgresql.conf to /etc/postgresql/8.4/main… Moving configuration file /var/lib/postgresql/8.4/main/pg_hba.conf to /etc/postgresql/8.4/main… Moving configuration file /var/lib/postgresql/8.4/main/pg_ident.conf to /etc/postgresql/8.4/main… Configuring postgresql.conf to use port 5432… …… * Starting PostgreSQL 8.4 database server [ OK ] Setting up postgresql (8.4.8-0ubuntu0.11.04) … Setting up postgresql-client (8.4.8-0ubuntu0.11.04) … Setting up postgresql-contrib-8.4 (8.4.8-0ubuntu0.11.04) … Setting up postgresql-contrib (8.4.8-0ubuntu0.11.04) … Processing triggers for libc-bin … 即创建了配置文件的位置为:/etc/postgresql/8.4/main/ 可执行程序为:

 sudo /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status}

PostgreSQL 默认配置了允许本地机器访问(local access)的权限,PostgreSQL 安装完毕,使用系统账户postgres以postgres角色登录数据库设置密码,命令:

sudo -u postgres psql。 登录sql命令界面后,修改 postgres 用户的密码(psql-PostgresQL的命令行客户端):

postgres=# ALTER ROLE postgres WITH ENCRYPTED PASSWORD ‘mypassword’; postgres=# \q 设置PostgreSQL启用远程访问 1. 这里设置允许远程连接权限:sudo vi /etc/postgresql/8.4/main/postgresql.conf

#listen_addresses = ‘localhost’ 去掉注释并修改为 listen_addresses = ‘*’ #password_encryption = on 去掉注释:password_encryption = on 2. 这里设置允许远程进行数据库操作:sudo vi /etc/postgresql/8.4/main/pg_hba.conf 最后添加一行(允许局域网ip段或其他任何ip):host all all 192.168.1.0/24 md5 其中24是CIDR地址,也可用网关代替。 ——————–最后pg_hba.conf可能为这样———————-

# Database administrative login by UNIX sockets local all postgres ident # TYPE DATABASE USER CIDR-ADDRESS METHOD # “local” is for Unix domain socket connections only local all all ident # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 host all all 192.168.1.0/24 md5 修改linux 用户postgres的密码

sudo passwd -d postgres 删除密码

sudo su postgres -c passwd 设置密码(su 切换当前用户到postgres) PostgreSQL创建用户和数据库 登录后使用sql语句:

create user “pytu” with password ‘mypassword’ nocreatedb;

CREATE DATABASE pytb OWNER pytu ENCODING ‘UTF-8′; 或命令行使用命令创建:

sudo -u postgres createuser -D -P dbuser 弹出设置密码

sudo -u postgres createdb -O dbuser mydb 最后经过上面的配置记得重启:sudo /etc/init.d/postgresql restart Ubuntu 10.04: /etc/init.d/postgresql-8.4 restart 安装 PostgreSQL 数据库管理工具 pgadmin3

sudo apt-get install pgadmin3 实现和php 下的Mysql 管理工具 phpmyadmin类似的可视化数据库管理界面,终端输入pgadmin3启动时,Ubuntu 11.04下出现如下错误提示,不知是何原因:

** (pgadmin3:5579): CRITICAL **: murrine_style_draw_flat_box: assertion `width >= -1′ failed 为了启用pgAdmin 的一些功能,必须运行下面这段脚本(To enable the functions of the pgAdmin utility, run a script against the postgres database):

sudo -u postgres psql -d postgres < /usr/share/postgresql/8.4/contrib/adminpack.sql 提示CREATE FUNCTION创建函数。不同Ubuntu版本,文件路径可能不同:如Ubuntu老版本使用pgAdmin8.1的路径是: /usr/share/postgresql/8.1/contrib/admin81.sql on current versions of Ubuntu. 安装 psycopg2 最后安装Python 的 PostgreSQL数据库驱动psycopg2:

sudo apt-get install python-psycopg2

验证psycopg2安装: >>> python >>> import psycopg2 >>> psycopg2.apilevel ’2.0′ Ubuntu 下,Python PostgreSQL 数据库的安装和配置基本就完成了,有点要注意的是使用django-admin新建django 项目时候,源文件最好不要放在apache 或其他web 服务器的document root下,可能会被人看到源代码。 FATAL: Ident authentication failed for user 问题 当新建一个python project 并在settings.py 中输入以下数据库信息之后: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'mydb', # Or path to database file if using sqlite3. 'USER': 'dbuser', # Not used with sqlite3. 'PASSWORD': 'mypassw', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } 测试PostgreSQL连接:

$ python manage.py shell >>> from django.db import connection >>> cursor = connection.cursor() 出现错误:

Traceback (most recent call last): File “”, line 1, in File “/usr/local/lib/python2.7/dist-packages/Django-1.3.1-py2.7.egg/django/db/backends/__init__.py”, line 250, in cursor cursor = self.make_debug_cursor(self._cursor()) File “/usr/local/lib/python2.7/dist-packages/Django-1.3.1-py2.7.egg/django/db/backends/postgresql_psycopg2/base.py”, line 140, in _cursor self.connection = Database.connect(**conn_params) OperationalError: FATAL: Ident authentication failed for user “dbuser” 出现这个错误的原因还是在于上面pg_hba.conf 文件的设置,Debian系(包括ubuntu)默认的pg_hba.conf 文件对于localhost本地机器的数据库访问方式是ident,它指的是只有Linux shell用户通过同名的postgreSQL 用户才能访问,也就是pg超级用户postgres 只能由linux 用户postgres 登录后操作。要解决类似OperationalError: FATAL: Ident authentication failed for user “postgres”的问题,有两种解决方法: 1. 在执行$ python manage.py shell之前先$su postgres 切换为postgres 用户 2. 修改pg_hba.conf 的客户端访问设置,将laocal 的访问由ident 改为trust,如:

# TYPE DATABASE USER CIDR-ADDRESS METHOD local all all trust 修改完pg_hba.conf设置记得重启pg。安装了pg_ctl 也可以用pg_ctl reload。

转载于:https://www.cnblogs.com/crazyant/archive/2012/06/27/2823868.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值