2019独角兽企业重金招聘Python工程师标准>>>
1.PostgreSQL
因为想添加个gis功能模块,看django推荐gis用PostgreSQL数据库,拿来试用下,安装的时候有几个注意的小问题。
1.To use the yum repository, you must first install the repository RPM. To do this, download the correct RPM from the repository RPM listing, and install it with commands like:
yum install http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-redhat93-9.3-1.noarch.rpm
2.Once this is done, you can proceed to install and update packages the same way as the ones included in the distribution.
yum install postgresql93-server postgresql93-contrib
service postgresql-9.3 initdb
chkconfig postgresql-9.3 on
centos需要手动初始化下然后设置默认自启动。
2.psycopg2
另外用psycopg2时也碰到几个问题
下载问题不大,直接去http://initd.org/psycopg/download/下载tar包就好了,安装的时候报了几次错。
首先一个pg_cong问题,然后一个libpq问题。去看他文档说了这么几点:
Psycopg is a C wrapper to the libpq PostgreSQL client library. To install it from sources you will need:
-
A C compiler.
-
The Python header files. They are usually installed in a package such as python-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.
-
The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.
-
The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATHdirectory. Having it in the PATHgreatly streamlines the installation, so try runningpg_config --version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually/usr/lib/postgresql/X.Y/bin/) and add it to the PATH:
$ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
You only need it to compile and installpsycopg2, not for its regular usage.
Note
The libpq header files used to compilepsycopg2should match the version of the library linked at runtime. If you get errors about missing or mismatching libraries when importingpsycopg2check (e.g. using ldd) if the modulepsycopg2/_psycopg.sois linked to the rightlibpq.so.
2.第二个,折腾了好久,centos里面python-dev 是python-devel,另外libpq-dev叫libpqxx-deve!
我屮艸芔茻!尼玛!
文档介绍忒不清楚!坑爹了!
这样就好了,yum install下,后面就一路顺利了。
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
libpqxx i686 1:4.0.1-1.rhel6 pgdg93 189 k
libpqxx-debuginfo i686 1:4.0.1-1.rhel6 pgdg93 772 k
libpqxx-devel i686 1:4.0.1-1.rhel6 pgdg93 91 k
Installing for dependencies:
postgresql93-devel i686 9.3.3-1PGDG.rhel6 pgdg93 1.4 M
Transaction Summary
================================================================================
Install 4 Package(s)
3.sqlalchemy+postgreSQL
安装完成之后, 系统中会多出一个名为 postgres 的用户, 这个用户用于登录数据库. 但无法直接用该用户在 shell 或 xdm 中登录, 须先用其它用户登录 shell, 然后su到该用户. 先为该用户设置一下密码
Password :
postgres@host : /home/ me $
could not change directory to "/home/me"
因为 postgres 这个用户无法读取当前用户的 home 目录. 觉得讨厌的话可以
psql ( version number )
Type "help" for help .
postgres =# input instructions here
这样会以 postgres 用户身份登录, 如果要输入密码就输入刚才为 postgres 用户设置的密码.
PostgreSQL默认的超级管理员密码是postgres
连接方法:psql -U postgres(注意,是大写的-U)
默认密码为空
修改密码的方法是,用psql登入管理:psql -U postgres
然后用这样的命令来修改密码:alter user postgres with password 'new password
这时 postgres 用户相当于数据库的根用户, 就像 root 用户之于 Linux 系统一样, 直接让应用程序使用 postgres 用户是比较危险的. 下面新建一个用户
CREATE ROLE
然后再建立一个新数据库, 并授权quanpower 这个用户可以使用该数据库
CREATE DATABASE
postgres =# GRANT ALL PRIVILEGES ON DATABASE SmartLinkCloud TO quanpower;
GRANT
如果上面每个句子输入之后没回显结果, 并且交互环境开头变为了postgres-#(注意 # 前是一个减号而非等号), 请查看一下句尾的分号是否漏掉了.
4.使用 SQLAlchemy 连接 Postgres
有关 SQLAlchemy 的基本使用请见 这里.编写这样一段 Python 代码, 来测试 Postgres 数据库连接是否可用
import sqlalchemy . orm as sqlorm
from sqlalchemy . ext . declarative import declarative_base as sqla_declarative_base
Base = sqla_declarative_base ()
engine = sqla . create_engine ( 'postgresql://psuer:qwerty@localhost:5432/mydb' , echo = True )
class Artist ( Base ):
__tablename__ = 'artist'
artist_id = sqla . Column ( 'id' , sqla . Integer , primary_key = True )
name = sqla . Column ( 'name' , sqla . String )
Base . metadata . bind = engine
Base . metadata . create_all ()
Session = sqlorm . scoped_session ( sqlorm . sessionmaker ( bind = engine ))
def save_artist ():
artist = Artist ( name = 'aki misawa' )
session = Session ()
try :
session . add ( artist )
session . flush ()
session . commit ()
finally :
session . close ()
if __name__ == '__main__' :
save_artist ()
- PROTOCOL: 协议名, 使用 Postgres 则为 postgres
- USERNAME/PASSWORD: 用户名, 即刚才配置的 psuser
- PORT: 端口, Postgres 默认端口为 5432 (想知道怎么修改端口请自行 Google)
- DATABASE_NAME: 数据库名, 即刚才配置的 mydb
如果上述代码运行一切顺利, 控制台应该输出完整的建表跟增加数据的语句. 之后在 psql 交互环境中执行
You are now connected to database "mydb" as user "postgres" .
id | name
----+-------------
1 | aki misawa
5.PostgreSQL与MySQL命令比较
|
6.几个报错整理:
1>新建了database,及user后,且授权后
postgres =# CREATE USER quanpower WITH PASSWORD 'XXXXXX';
CREATE ROLE
postgres =# CREATE DATABASE SmartLinkCloud;
CREATE DATABASE
postgres =# GRANT ALL PRIVILEGES ON DATABASE SmartLinkCloud TO quanpower;
GRANT
用psql -U quanpower -d SmartLinkCloud -h localhost登录,报错:
postgres Peer authentication failed for user “quanpower”:
在配置之前需将postgresql的端口号5432在iptables下开放。
开放方法参考:http://blog.csdn.net/ivan820819/archive/2009/02/03/3860163.aspx
yum安装postgresql后的安装路径为:/var/lib/pgsql下,主要配置文件在其data文件夹下,进入data文件夹
1、修改postgresql.conf文件
如果想让PostgreSQL监听整个网络的话,将listen_addresses前的#去掉,并将listen_addresses = 'localhost'改成listen_addresses = '*'
2、修改pg_hba.conf
sudo vi /var/lib/pgsql/9.3/data/pg_hba.conf
这个文件最后有一个列表,它决定了分派了每一个用户的权限,以及认证方式。格式是“Type Database User Address Method”,要注意的是method最好写md5。
在列表后追加一行:host all all 192.168.1.0/24 password
3、修改postgres用户密码:passwd postgres
4、暂时将pg_hba.conf中,本机的认证方式改为trust,切换当前用户为postgres:su postgres
5、用psql登录PostgreSQL系统,“SELECT * FROM pg_shadow;”,发现这个表里的postgres这个用户根本还没有存储密码;于是,再“ALTER USER postgres PASSWORD '它的密码';
6、重启服务/etc/init.d/postgresql-9.3 restart,连接成功。
PS:
Q. I've installed Postgresql under Red Hat Enterprise Linux 5.x server. I've created username / password and database. But when I try to connect it via PHP or psql using following syntax:
psql -d myDb -U username -W
It gives me an error that read as follows:
psql: FATAL: Ident authentication failed for user "username"
How do I fix this error?
A. To fix this error open PostgreSQL client authentication configuration file /var/lib/pgsql/data/pg_hba.conf :
# vi /var/lib/pgsql/data/pg_hba.conf
This file controls:
- Which hosts are allowed to connect
- How clients are authenticated
- Which PostgreSQL user names they can use
- Which databases they can access
By default Postgresql uses IDENT-based authentication. All you have to do is allow username and password based authentication for your network or webserver. IDENT will never allow you to login via -U and -W options. Append following to allow login via localhost only:
local all all trust
host all 127.0.0.1/32 trust
Save and close the file. Restart Postgresql server:
# service postgresql restart
Now, you should able to login using following command:
$ psql -d myDb -U username -W