目錄
- homebrew 安裝
- 啟動和關閉 postgresql
- 創建資料庫和賬戶
- 登陸控制臺指令
一.homebrew 安裝
安裝命令
eternity@TheEternitydeMacBook-Pro ~ % brew install postgresql
eternity@TheEternitydeMacBook-Pro ~ % psql --version
psql (PostgreSQL) 12.3
初始化
initdb /usr/local/var/postgres
如果出現如下提示,可以跳過此步
The files belonging to this database system will be owned by user "eternity".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
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.
initdb: error: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
二.啟動和關閉 postgresql
設成開機啟動 PostgreSQL:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
啟動 PostgreSQL:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
關閉 PostgreSQL:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
三.創建資料庫和賬戶
1.初始化資料庫及登錄
mac安裝PostgreSQL后不會創建用戶名資料庫,執行命令:
createdb
如果不執行 createdb,會報錯:psql: error: could not connect to server: FATAL: database "用戶名" does not exist
然后登錄PostgreSQL控制臺:
psql
示例:
eternity@TheEternitydeMacBook-Pro postgres % psql
psql (12.3)
Type "help" for help.
eternity=#
psql連接資料庫默認選用的是當前的系統用戶
使用\l命令列出所有的資料庫,看到已存在用戶同名資料庫、postgres資料庫,但是postgres資料庫的所有者是當前用戶,沒有postgres用戶,
2.創建用戶及資料庫
①創建postgres用戶
CREATE USER postgres WITH PASSWORD '123456';
②洗掉默認生成的postgres資料庫
DROP DATABASE postgres;
③創建屬于postgres用戶的postgres資料庫
CREATE DATABASE postgres OWNER postgres;
④將資料庫所有權限賦予postgres用戶
GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;
⑤給postgres用戶添加創建資料庫的屬性
ALTER ROLE postgres CREATEDB;
這樣就可以使用postgres作為資料庫的登錄用戶了,并可以使用該用戶管理資料庫
四.登陸控制臺指令
psql -U [user] -d [database] -h [host] -p [port]
-U指定用戶,-d指定資料庫,-h指定服務器,-p指定埠
完整的登錄命令,比如使用postgres用戶登錄
psql -U postgres -d postgres
之前我們直接使用psql登錄控制臺,實際上使用的是預設資料
user:當前mac用戶
database:用戶同名資料庫
主機:localhost
埠號:5432,postgresql的默認埠是5432
常用控制臺指令
\password:設定當前登錄用戶的密碼
\h:查看SQL命令的解釋,比如\h select,
\?:查看psql命令串列,
\l:列出所有資料庫,
\c [database_name]:連接其他資料庫,
\d:列出當前資料庫的所有表格,
\d [table_name]:列出某一張表格的結構,
\du:列出所有用戶,
\e:打開文本編輯器,
\conninfo:列出當前資料庫和連接的資訊,
\password [user]: 修改用戶密碼
\q:退出
使用PostgreSQL
現在來簡單的學習一下使用PostgreSQL,以下命令都在postgres=# 環境下
修改用戶密碼
之前我們用命令CREATE USER postgres WITH PASSWORD 'XXXXXX';創建了postgres用戶,現在我們來修改該用戶的密碼:
ALTER USER postgres WITH PASSWORD 'XXXXXX'
出現ALTER ROLE, 代表修改角色成功
創建和洗掉資料庫用戶
創建user1用戶:CREATE USER user1 WITH PASSWORD 'XXXX'
查看資料庫用戶串列:\du
洗掉資料庫用戶:drop user user1;
創建和洗掉資料庫
創建資料庫:create database testdb;
查看資料庫串列:\l
洗掉資料庫:drop database db1;
創建和洗掉資料表
選擇資料庫:\c DatabaseName,比如\c testdb
創建資料庫表:CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);
洗掉資料庫表: drop table company;
查看資料庫資訊:\d
查詢資料:select * from company
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/194.html
標籤:OS X
下一篇:單片機及其作業原理粗略介紹
