Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

分類: 未分類

在 PostgreSQL 建立 admin 帳號

Posted on 2022 年 2 月 24 日2022 年 2 月 24 日 By 日落 在〈在 PostgreSQL 建立 admin 帳號〉中有 1 則留言

PostgreSQL 的使用者權限管理和 MySQL 差很多,花了好一陣子才搞懂。

一開始操作都必須使用 postgres 的使用者身份來操作,所以都會需要 sudo:

# 建立預備要給予 admin 權限的帳號
# 如果不想要 sudo 則設定成 Linux 相同帳號名稱
sudo -u postgres createuser zeroplex

再來是要幫上面新增的使用者設定 role,這邊需要進入 PostgreSQL 的 console 中下指令:

sudo -u postgres psql
psql (14.2)
Type "help" for help.

-- 透過 \du 來列出現有的使用者與角色
postgres=# \du
                                    List of roles
 Role name |                         Attributes                         | Member of  
-----------+------------------------------------------------------------+------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 zeroplex  |                                                            | {}

從上表的內容,可以看出剛剛新增的帳號什麼權限都沒有,但是預設帳號 postgres 擁有 Superuser 的權限。

若要建立的帳號擁有 admin 權限 (在 PostgreSQL 中叫做 superuser),則須透過 alter user 來授予權限:

postgres=# alter user zeroplex with superuser;
ALTER ROLE

-- 再次列出使用者與其角色 (role)
postgres=# \du
                                    List of roles
 Role name |                         Attributes                         | Member of  
-----------+------------------------------------------------------------+------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 zeroplex  | Superuser                                                  | {}

除了 superuser 以外,還有其他不同權限的角色可以指派,請參考 PostgreSQL alter user 的說明。

接著就可以不用 sudo 直接下 PostgreSQL 管理相關指令:

$ createdb test

注意:使用者如果擁有 superuser 權限以後,下管理相關指令是不需要 sudo 的,因此必須承擔意外造成的風險。個人建議還是切換成 sudo -u postgres 以後在進行操作比較保險。

Tags:PostgreSQL

Customize HTTP header in Unit Test on Lumen

Posted on 2022 年 2 月 23 日2022 年 2 月 23 日 By 日落 在〈Customize HTTP header in Unit Test on Lumen〉中有 1 則留言

按照 Lumen 文件,在 unit test 中建立 HTTP request 要呼叫 $this->call(),但沒辦法自訂 header 內容。

細看實作 Laravel\Lumen\Testing\Concerns\MakesHttpRequests::call():

/**
 * Call the given URI and return the Response.
 *
 * @param  string  $method
 * @param  string  $uri
 * @param  array  $parameters
 * @param  array  $cookies
 * @param  array  $files
 * @param  array  $server
 * @param  string  $content
 * @return \Illuminate\Http\Response
 */
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $this->currentUri = $this->prepareUrlForRequest($uri);

    $symfonyRequest = SymfonyRequest::create(
        $this->currentUri, $method, $parameters,
        $cookies, $files, $server, $content
    );

    $this->app['request'] = LumenRequest::createFromBase($symfonyRequest);

    return $this->response = TestResponse::fromBaseResponse(
        $this->app->prepareResponse($this->app->handle($this->app['request']))
    );
}

從 function parameter 和 return,大概就知道使用這個方法沒辦法塞 header 值。

但是若透過 SymfonyRequest 的話,則可以設定 header 內容:

$request = SymfonyRequest::create();
$request->headers->set('acccess_token', 'ooxx');

因此,若需要自訂 header,則必須 override 原本的 call() 方法。以下是自己改寫的 trait:

use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

trait ApiTokenRequestTrait
{
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null, $headers = [])
    {
        $symfonyRequest = SymfonyRequest::create(
            $uri,
            $method,
            $parameters,
            $cookies,
            $files,
            $server,
            $content
        );

        foreach ($headers as $key => $value) {
            $symfonyRequest->headers->set($key, $value);
        }

        $this->app['request'] = \Laravel\Lumen\Http\Request::createFromBase($symfonyRequest);

        return TestResponse::fromBaseResponse(
            $this->app->prepareResponse($this->app->handle($this->app['request']))
        );
    }
}
Tags:Laravel, Lumen, PHP

Automatically mount tmpfs on Linux

Posted on 2022 年 2 月 22 日2022 年 2 月 22 日 By 日落 在〈Automatically mount tmpfs on Linux〉中尚無留言

編輯 /etc/fstab,建立新的一行設定:

tmpfs    /media/ramdisk    tmpfs    defaults,size=16M,mode=1777    0  0

第二個欄位是 mount point。另外在 option 欄位可以透過 size 設定分配的檔案系統大小。2

設定完成後,可以透過 mount -a 立即掛載。

Tags:Linux

YouTube 影片格式筆記

Posted on 2022 年 2 月 18 日2022 年 2 月 18 日 By 日落 在〈YouTube 影片格式筆記〉中尚無留言

前陣子錄影上傳到 YouTube:

高鐵嘉義到彰化區間漂亮的田野風景

發現使用舊有的 yt-dlp 參數會抓不到想要的影片格式,透過 --list-formats 和 --list-subs 工具以後,才知道 YouTube 的影片格式有這麼多種,難怪上傳完成到上架要等很久。

More “YouTube 影片格式筆記” »

Tags:YouTube, 生活雜記

在 Laravel 建立並自動更新 sitemap.xml

Posted on 2022 年 2 月 17 日2022 年 2 月 17 日 By 日落 在〈在 Laravel 建立並自動更新 sitemap.xml〉中有 1 則留言

sitemap.xml 應該算是 SEO 中相當重要的一個檔案,告訴搜尋引擎到底網站中有哪些資源是公開瀏覽的,可惜 Laravel framework 並沒有支援這項功能,必須透過其他工具達成。

以下介紹二個目前看到比較欣賞的解決方法。

More “在 Laravel 建立並自動更新 sitemap.xml” »

Tags:Laravel, PHP, 網路架站

文章分頁

上一頁 1 ... 41 42 43 ... 318 下一頁

其他

關於我  (About me)

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.