最近没事研究postgreSQL和学习python3,于是在自己的mac电脑上试验了一下postgreSQL的使用。由于不想用安装版的postgreSQL版本,所以直接使用了解压版的postgreSQL版本(下载地址https://www.enterprisedb.com/download-postgresql-binaries )。下载到zip包后直接解压,目录结构如下:
这里也就懒得设置什么环境变量了,直接开始干吧。
先进入bin目录下,执行initdb文件,如下:
YZQ-MacBook-Pro:bin yinzeqiang$ ./initdb -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -E utf8
The files belonging to this database system will be owned by user "yinzeqiang".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
fixing permissions on existing directory /Users/yinzeqiang/softDev/pgsql/dataFiles ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /Users/yinzeqiang/softDev/pgsql/dataFiles/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
1.initdb命令
./initdb -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -E utf8 命令,我的理解就是搞一个数据文件存储目录初始化数据库吧。-D后跟数据库数据文件存储路径,-E后跟编码。2.pg_ct命令
pg_ct命令可以开始数据库服务或者关闭数据库。
./pg_ctl -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -l /Users/yinzeqiang/softDev/pgsql/dataFiles/postgrepsql.log start命令,开始数据库服务。-D后跟数据库数据文件存储
路径,-l后跟日志文件地址。
./pg_ctl stop -w -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -m smart命令结束数据库服务。-D后跟数据库数据文件存储路径.-m后跟数据库停止或者重启的模式:
一共有三种模式:
smart quit after all clients have disconnected
fast quit directly, with proper shutdown
immediate quit without complete shutdown; will lead to recovery on restart
3.createdb和createuser命令
YZQ-MacBook-Pro:bin yinzeqiang$ ./createuser -a -d -e -P python3learner
Enter password for new role:
Enter it again:
CREATE ROLE python3learner PASSWORD 'md5fb444479c9daa2b26fa4d6c045699070' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;
createuser命令创建用户,用法:createuser [OPTION]... [ROLENAME]
OPTION可用参数如下:
[-A]:不允许此用户创建其他用户;
[-d]:允许此用户创建数据库;
[-D]:不允许此用户创建数据库;
[-e]:将执行过程显示到Shell上;
[-P]:创建用户时,同时设置密码;
[-h 主机名]:为某个主机上的Postgres创建用户;
[-p port]:与-h参数一同使用,指定主机的端口。
createdb命令用于创建数据库,用法:createdb [OPTION]... [DBNAME] [DESCRIPTION]。如下:
./createdb -E utf8 testdb 创建名为testdb的数据库,-E后跟数据库编码
4.psql命令
psql可以连接数据库,如下:
YZQ-MacBook-Pro:dataFiles yinzeqiang$ psql -d testdb
psql (9.5.5)
Type "help" for help.
testdb=# \d
List of relations
Schema | Name | Type | Owner
--------+-----------------------------------+----------+----------------
public | auth_group | table | python3learner
public | auth_group_id_seq | sequence | python3learner
public | auth_group_permissions | table | python3learner
public | auth_group_permissions_id_seq | sequence | python3learner
public | auth_permission | table | python3learner
psql -d testdb连接到testdb数据库。
5.所有命令可以通过 '命令 --help'的形式查看用法,稍微懂一点英语都可以看懂
(IT菜鸟,有错误请指正)