PostgreSQL簡單使用

PostgreSQL簡介

  1. 安裝

    // 搜索顯示可以使用
    brew search postgresql
    postgresql      postgresql@9.4      postgresql@9.5      postgresql@9.6
    // 安裝方式
    brew install postgresql
    brew install postgresql@10.1
    // 安裝成功之后提示
    // 升級之前版本的數(shù)據(jù)
    brew postgresql-upgrade-database
    // 開啟和關閉postgresql服務
    brew services start postgresql 
    brew services stop postgresql
    // 或者這種不需要背景服務器(background service,直譯背景服務器)的方式
    pg_ctl -D /usr/local/var/postgres start 
    pg_ctl -D /usr/local/var/postgres stop
    // 其他可供使用的命令
    // 可以獲得postgres的詳細用法 <postgres is the PostgreSQL server>
    postgres —-help
    // 開啟了服務且監(jiān)聽輸出一些日志
    postgres -D /usr/local/var/postgres
    // 可以獲得pg_ctl的詳細用法 <pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server>
    pg_ctl --help
    // 可以獲得psql的詳細用法 <psql is the PostgreSQL interactive terminal.>
    psql --help 
    // 打開一個名字是**的數(shù)據(jù)庫,不帶參數(shù),則打開默認數(shù)據(jù)庫
    psql ’database name’ 
    
  2. 可以直接終端使用命令

    createdb
    createuser
    dropdb
    dropuser
    pg_ctl
    postgres
    psql
    initdb
    pg_archivecleanup
    pg_basebackup
    pg_config
    pg_controldata
    pg_dump
    pg_dumpall
    pg_isready
    pg_receivewal
    pg_recvlogical
    pg_resetwal
    pg_restore
    pg_rewind
    pg_standby
    pg_test_fsync
    pg_test_timing
    pg_upgrade
    pg_waldump
    pgbench
    postmaster
    reindexdb
    vacuumdb
    vacuumlo
    clusterdb
    ecpg
    oid2name
    // 這些命令的具體用法,可以直接
    命令名字 --help . 例子:psql --help 去獲取更詳細的命令用法
    
  3. 更具體的使用例子

    // 初始化一個新的數(shù)據(jù)庫
    pg_ctl init -D /Users/zsk/Desktop/mydb
    
    The files belonging to this database system will be owned by user "zsk".
    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.
    
    fixing permissions on existing directory Desktop/mydb ... ok
    creating subdirectories ... ok
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    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:
    
        /usr/local/Cellar/postgresql/10.1/bin/pg_ctl -D Desktop/mydb -l logfile start
        
    // 自己數(shù)據(jù)庫的開啟使用
    pg_ctl -D /Users/zsk/Desktop/mydb start
    
    另開一個終端窗口
    createdb mydb 創(chuàng)建一個名字是mydb的數(shù)據(jù)庫
    dropdb mydb 刪除一個名字是mydb的數(shù)據(jù)庫
    psql -l 查詢當前開區(qū)數(shù)據(jù)庫服務器下的數(shù)據(jù)庫列表
    psql postgres 進入其中一個名字為postgres的數(shù)據(jù)庫
    // 代碼
    psql postgres
    postgres=#help 看到一些幫助信息
    postgres=#create table numbers (age int);記得末尾的分號。
    postgres=#\dt 查看表格列表
    postgres=#table numbers; 查看表內(nèi)容
    postgres=#drop table numbers; 刪除表numbers
    postgres=# \dT[s+] 數(shù)據(jù)類型列表
    postgres=# 直接使用sql語句
    postgres=#\q 退出
    
  4. javascript的調用

    var pg = require('pg');
    var connectString = 'postgres://localhost:5432/mydb';
    var client = new pg.Client(connectString);
    client.connect();
    client.query('SELECT * from myclass', (err, res) => {
      console.log(err, res.rows)
      client.end()
    })
    
    client.query("insert into myclass values ('mycall-6班',105,'my teach6',60)", (err, res) => {
        console.log(err, res);
        client.end();
    })
    

參考資料
官方網(wǎng)站
官方文檔10.0版本
社區(qū)翻譯文檔
中文翻譯文檔
基本使用
sql語法簡單介紹
Mac安裝引導頁面
postgresql的安裝包列表
pg使用文檔

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容