LoginSignup
3
3

More than 5 years have passed since last update.

Windows環境でPostgreSQL

Last updated at Posted at 2015-07-28

サーバー側

インストール

PostgreSQL 9.4 をインストールする。

  1. http://www.enterprisedb.com/products-services-training/pgdownload から Version 9.4 の Win x86-64 のインストーラをダウンロード
  2. postgresql-9.4.*-windows-x64.exe を実行
  3. Installation Directory: D:\PostgreSQL\9.4
  4. Data Directory: D:\PostgreSQL\9.4\data
  5. Password: パスワードを設定(ユーザー「postgres」のパスワードになる)
  6. Port: そのまま(5432だった)
  7. Advanced Options
    • Locale: C
  8. 「Stack Builder ...」のチェックを外して Finish

別の端末からの接続を許可

pg_hba.conf に許可したい端末のアドレスを設定。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             <アドレス>                 md5

サービス「postgresql-x64-9.4」を再起動

クライアント側

  1. Visual Studio でプロジェクトを作成
    • テンプレート: C#/Windowsフォームアプリケーション
    • プロジェクト名: PostgresTest
  2. パッケージマネージャコンソールで以下を実行
    • Install-Package Npgsql -Version 2.2.5 (latest stable を使用)
    • Install-Package Dapper
  3. Form1 にボタンを追加して、クリックするとサーバ側の現在時を取得するようにする
using System;
using System.Linq;
using System.Windows.Forms;
using Npgsql;
using Dapper;

namespace PostgresTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime resultDate;
            using (var conn = new NpgsqlConnection("Server=<サーバー名>;Database=postgres;Uid=postgres;Pwd=<パスワード>"))
            {
                conn.Open();
                resultDate = conn.Query<DateTime>("SELECT NOW();").Single();
            }

            MessageBox.Show(resultDate.ToString());
        }
    }
}

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3