极简PGSql安装实践
2020-10-15 本文已影响0人
万州客
这次集团竞赛,也涉及了PGSql,一起弄弄。
一,下载
https://www.enterprisedb.com/download-postgresql-binaries
现在能找到的二进制安装包,只有下面这个了,将就,差不多。postgresql-10.14-1-linux-x64-binaries.tar.gz
下载之后,解压到一个测试用目录,然后进入这个目录。后面的演示,都是基于这个目录。
二,初始化数据
新建一个数据目录
mkdir data
初始化数据库
bin/initdb -D data
输出如下:
The files belonging to this database system will be owned by user "docker".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... PRC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... 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:
bin/pg_ctl -D data -l logfile start
三,启动PGSql服务
bin/pg_ctl -D data -l logfile start
输出如下:
waiting for server to start.... done
server started
四,登陆数据库
bin/psql -h localhost -d postgres -U docker -p 5432
由于本地登陆不需要密码,输出如下:
psql.bin (10.14)
Type "help" for help.
postgres=#
在这个命令行提示符下,就可以大展拳脚啦~
五,常用操作
create database testdb: 新建数据库
\l :查看数据库列表
\du:查看用户
\dt:查看表
\d:查看表结构
\c:切换数据库
\di: 查看索引
\c - user:切换用户
\q: 退出命令行
postgres=# create database testdb
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+-------------+-------------+-------------------
postgres | docker | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | docker | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/docker +
| | | | | docker=CTc/docker
template1 | docker | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/docker +
| | | | | docker=CTc/docker
testdb | docker | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
postgres=# \dt
Did not find any relations.
postgres=# \d
Did not find any relations.
postgres=# \c testdb
You are now connected to database "testdb" as user "docker".
testdb=# \dt
Did not find any relations.
testdb=# \di
Did not find any relations.
六,启停命令
停止服务
bin/pg_ctl -D data -l logfile stop
重启服务
bin/pg_ctl -D data -l logfile restart