Pgql 數據庫登錄、操作數據庫 、設置密碼、導入數據、


 

設置默認密碼

設置默認密碼
[root@VM_0_2_centos start-scripts]# su - postgres
Last login: Fri Nov 12 16:49:36 CST 2021 on pts/3
-bash-4.2$  psql -U postgres
psql (12.5)
Type "help" for help.

postgres=# ALTER USER postgres with encrypted password 'postgres'; 
ALTER ROLE
postgres=# 

 

導入數據文件

 

 

 

導出數據:

[wiew@szyhdb1c ~] cd /pgsoft/pg12.5/bin
[view@szyhdblc bin] ./pg_dump -h  IP -p 5432 -U 用戶名稱 -d 數據庫名稱 > /home/view/szsfs_db_2021/test_pd_dump_20211220/szsfs20211220.bak
Password:
[view@szyhdblc bin] 

 

 

 

    [view@szyhdb1c ~]# cd /usr/local/pgsql/bin/
    [view@szyhdb1c bin]#./psql -s 數據庫名稱  -f /home/veiw/szsfs20220328_SqlUpdate.sql    
     
   ==========================注:如果上述腳本執行操作出現了以下錯誤內容:請查看該內容進行操作==================================================================================
   ==|               [root@localhost bin]# clear                                                                                                          ======================
   ==|                [root@localhost bin]# cd "/usr/local/pgsql/bin"                                                                                       ======================    
   ==|                [root@localhost bin]# ./psql -s 數據庫名稱  -f /home/postgres/szsfs20220328_SqlUpdate.sql                                         ======================
   ==|                psql: error: FATAL:  role "root" does not exist                                                                                         ======================
   ==|                [root@localhost bin]# su - postgres                                                                                                  ======================
   ==|                Last login: Mon Mar 28 10:57:33 CST 2022 on pts/3                                                                                    ======================
   ==|                -bash-4.2$ psql -U postgres                                                                                                          ======================
   ==|                psql (12.5)                                                                                                                          ======================
   ==|                Type "help" for help.                                                                                                                ======================
   ==|                                                                                                                                                   ======================
   ==|                postgres=# create user root with password 'passwordSzsfsroot';                                                                       ======================
   ==|                CREATE ROLE                                                                                                                          ======================
   ==|                postgres=# grant all privileges on database szsfs20220220 to root;                                                                   ======================
   ==|                GRANT                                                                                                                                ======================
   ==|                postgres=# \q                                                                                                                        ======================
   ==|                could not save history to file "/home/postgres/.psql_history": No such file or directory                                             ======================
   ==|                -bash-4.2$                                                                                                                           ======================
   ==|                -bash-4.2$ exit                                                                                                                      ======================
   ==|                [root@localhost bin]# ./psql -s 數據庫名稱  -f /home/postgres/szsfs20220328_SqlUpdate.sql 
   ==|                                         ***(Single step mode: verify command)*******************************************
   ==|                                        alter table t_wx_reconciliation add column iSCHECKdate date ;
   ==|                                        ***(press return to proceed or enter x and return to cancel)********************
   ==|
   ==|                                        psql:/home/postgres/szsfs20220328_SqlUpdate.sql:3: ERROR:  must be owner of table t_wx_reconciliation
   ==|                                        ***(Single step mode: verify command)*******************************************
   ==|                                        comment on COLUMN  t_wx_reconciliation.ischeckdate is '繳費通知書對賬時間';
   ==|                                        ***(press return to proceed or enter x and return to cancel)********************
   ==|
   ==|                                        psql:/home/postgres/szsfs20220328_SqlUpdate.sql:4: ERROR:  must be owner of relation t_wx_reconciliation
   ==|                                        ***(Single step mode: verify command)*******************************************
   ==|                                        alter table t_paynote add column iSCHECKdate date ;
   ==|                                        ***(press return to proceed or enter x and return to cancel)********************
   ==|
   ==|                                        psql:/home/postgres/szsfs20220328_SqlUpdate.sql:5: ERROR:  must be owner of table t_paynote
   ==|                                        ***(Single step mode: verify command)*******************************************
   ==|                                        comment on COLUMN t_paynote.ischeckdate is '繳費通知書對賬時間';
   ==|                                        ***(press return to proceed or enter x and return to cancel)********************
   ==|
   ==|                                        psql:/home/postgres/szsfs20220328_SqlUpdate.sql:6: ERROR:  must be owner of relation t_paynote
   ==|                                        ***(Single step mode: verify command)*******************************************
   ==|                                        /**
   ==|
   ==|                                        # 注意:如果在生產環境執行上述腳本內容出現以下內容,則表示該腳本已經執行過了。無效關注
   ==|
   ==|                                        szsfs20220220=# alter table t_wx_reconciliation add column iSCHECKdate date ;
   ==|                                        ¹¦對賬時間';ERROR:  column "ischeckdate" of relation "t_wx_reconciliation" already exists
   ==|                                        szsfs20220220=# comment on COLUMN  t_wx_reconciliation.ischeckdate is '繳費通知書對賬時間';
   ==|                                        COMMENT
   ==|
   ============================================================================================================================================================================
  
   驗證腳本發布執行情況:
    [view@szyhdb1c ~]# clear
    [view@szyhdb1c ~]# cd /usr/local/pgsql/bin/
    [view@szyhdb1c bin]# ./psql -h IP -p 5432 -U  用戶名稱 -d 數據庫名稱
       Password for user szsfs:                                                #注:密碼請與鄭猛聯系
       psql (12.5)
       Type "help" for help.
       szsfs20220220=# 
   

    

 

 

 

linux下 postgres實現導出和導入

 

用postgres 的pg_dump可以實現從從postgres數據庫中導出數據。
[1]只導出所有對象的數據庫結構
   C:\>pg_dump -f DDDDDD.sql -i  -C -E UTF8 -n public -s  -U portal -h localhost -W portal
   -i 是為了兼容數據庫版本
   -C 包括創建數據庫的語句
   -E 設定導出數據的編碼
   -n 是Scheme的名稱
   -U 是用戶名稱
   -h 是數據庫服務器的名稱
   -W 是用強制密碼驗證

   -s 只導出數據庫結構

   最后一個參數,當然就是數據庫名稱了

[2]導出所有對象的數據庫結構和數據
   C:\>pg_dump -f DDDDDD.sql -i  -C -E UTF8 -n public  -U portal -h localhost -W portal
   沒有-s參數
[3]只導出所有的表數據
   C:\>pg_dump -f DDDDDD.sql -i -a -C -E UTF8 -n public  -U portal -h localhost -W portal
   -a 只導出數據
數據導入
[1]c:\psql -f DDDDDD.sql -h 192.168.1.233 -U myuser -W myportal
   執行就可以實現導入了。
   如果數據庫myportal 不存在,要先創建數據庫
   createdb -U postgres -h 192.168.1.233  myportal
   然后再執行上面的導入語句就可以了。
[2]psql -hlocalhost -U myuser -d myportal < DDDDDD.sql
   執行語句導入數據就可以了。

 

 

 

 

 

登錄Pgsql 數據庫

To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Mon Mar 28 09:30:23 2022 from 192.168.255.4 
[root@localhost home]# cd /usr/local/pgsql/bin/
[root@localhost bin]# ./psql -h 127.0.0.1 -p 5432 -U szsfs20220220 -d szsfs20220220
psql (12.5)
Type "help" for help.

 
szsfs20220220=#  

 

 

 

[root@localhost bin]# clear
[root@localhost bin]# su - postgres
Last login: Mon Mar 28 10:02:24 CST 2022 on pts/1
-bash-4.2$ 
-bash-4.2$ psql -U postgres
psql (12.5)
Type "help" for help.

postgres=# \c szsfs20220220
You are now connected to database "szsfs20220220" as user "postgres".
szsfs20220220=# \d t_paynote
                                Table "public.t_paynote"
        Column         |              Type              | Collation | Nullable | Default 
-----------------------+--------------------------------+-----------+----------+---------
 pn_no                 | character varying(35)          |           | not null | 
 unit_no               | character varying(30)          |           | not null | 
 unit_name             | character varying(200)         |           |          | 
 payer_name            | character varying(300)         |           |          | 
 payer_accountno       | character varying(50)          |           |          | 
 receivable_amt        | numeric(16,2)                  |           |          | 
 pn_received_amt       | numeric(16,2)                  |           |          | 
 overdue_amt           | numeric(16,2)                  |           |          | 
 create_tm             | timestamp(6) without time zone |           |          | 
 paid_tm               | timestamp(6) without time zone |           |          | 
 rt_no                 | character varying(6)           |           |          | 
 receipt_no            | character varying(20)          |           |          | 
 account_no            | character varying(50)          |           |          | 
 bank_no               | character varying(30)          |           |          | 
 bank_name             | character varying(200)         |           |          | 
 st_type               | numeric(10,0)                  |           |          | 
 business_type         | numeric(10,0)                  |           |          | 
 status                | numeric(10,0)                  |           |          | 
 bind_status           | numeric(10,0)                  |           |          | 
 remark                | character varying(200)         |           |          | 
 proctime              | timestamp(6) without time zone |           |          | 
 procuserid            | numeric(10,0)                  |           |          | 
 detailiteminfo        | character varying(500)         |           |          | 
 fullcheckcode         | character varying(5)           |           |          | 
 numcheckcode          | character varying(5)           |           |          | 
 additionalamount      | numeric(16,2)                  |           |          | 
 paytype               | numeric(10,0)                  |           |          | 
 batchno               | numeric(10,0)                  |           |          | 
 acc_file_name         | character varying(200)         |           |          | 
 wt_file_name          | character varying(80)          |           |          | 
 acc_file_up_day       | timestamp(6) without time zone |           |          | 
 wt_file_down_day      | timestamp(6) without time zone |           |          | 
 payee_no              | character varying(30)          |           |          | 
 payee_name            | character varying(100)         |           |          | 
 id                    | numeric(20,0)                  |           | not null | 
 refund_bank_name      | character varying(200)         |           |          | 
 refund_bank_no        | character varying(30)          |           |          | 

 

 

修改數據名稱、查看數據庫信息

[root@192 ~]# su - postgres
Last login: Mon Mar 28 14:27:10 UTC 2022
-bash-4.2$ psql -U szsfs20220220
psql (12.5)
Type "help" for help.
^
szsfs20220220=# update pg_database set datname ='szsfs20220329' where datname='szsfs20220220';
UPDATE 1
szsfs20220220=# \c postgres
You are now connected to database "postgres" as user "szsfs20220220".
postgres=# \c szsfs20220220
FATAL:  database "szsfs20220220" does not exist
postgres-# \l
                                           List of databases
     Name      |     Owner     | Encoding |   Collate   |    Ctype    |        Access privileges        
---------------+---------------+----------+-------------+-------------+---------------------------------
 postgres      | postgres      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 szsfs20220329 | szsfs20220220 | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/szsfs20220220              +
               |               |          |             |             | szsfs20220220=CTc/szsfs20220220+
               |               |          |             |             | root=CTc/szsfs20220220
 template0     | postgres      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres                    +
               |               |          |             |             | postgres=CTc/postgres
 template1     | postgres      | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres                    +
               |               |          |             |             | postgres=CTc/postgres
(4 rows)

postgres-# 


Connection closed by foreign host.

Disconnected from remote host(confluence) at 05:53:41.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM