LoginSignup
1
0

More than 3 years have passed since last update.

Windows7端末のVB.NETからVagrant(CentOS7)+PostgreSQL9.6に接続するまで

Last updated at Posted at 2019-09-02

概要

自分用のメモ兼、同じ環境を作りたい人用。
色々な方のものを流用して一連の流れとしてまとめました。

Formアプリケーションでの動作を想定しています。

流れ

Vagrant1.9.8をインストール
VirtualBox ver.5.1.32をインストール
VagrantのCentOS7にPostgreSQL9.6を設定
VisualBasic2019をインストール
VisualBasic2019からPostgreSQL9.6に接続

Vagrant1.9.8をインストール

下記リンクの「vagrant_1.9.8_x86_64.msi」をダウンロードしてインストール。
https://releases.hashicorp.com/vagrant/1.9.8/

VirtualBox ver.5.1.32をインストール

下記リンクの「VirtualBox 5.1.32 (released January 15 2018)」の「Windows hosts x86/AMD64」をダウンロードしてインストール。
https://www.virtualbox.org/wiki/Download_Old_Builds_5_1

VagrantのCentOS7にPostgreSQL設定

CentOS7の設定

### 
vagrant box add centos/7

### 作業場所に移動 ※任意の場所
cd C:\Users\ユーザー名\Documents\Work\01_Vagrant

### Vagrantfileを格納するフォルダを作成 ※任意のフォルダ名
mkdir centos7
cd centos7

### 
vagrant init centos/7

### Vagrantの立ち上げ
vagrant up

PostgreSQL9.6の設定

### 
vagrant ssh

### パッケージをダウンロード
sudo yum -y install wget
sudo wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

### パッケージをインストール
sudo yum -y install pgdg-centos96-9.6-3.noarch.rpm
sudo yum -y install postgresql96-server postgresql96-devel postgresql96-contrib

### PostgreSQL9.6のデータベースを初期化と起動
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
sudo systemctl start postgresql-9.6

### PostgreSQL9.6の設定を変える
sudo vi /var/lib/pgsql/9.6/data/postgresql.conf
59行目付近:  listen_addresses = '*'
63行目付近: port = 5432
※コメントアウトは外す

sudo vi /var/lib/pgsql/9.6/data/pg_hba.conf
86行目付近: host    all             all             all                     trust

### PostgreSQLの再起動
sudo systemctl restart postgresql-9.6

### PostgreSQLの自動起動設定
sudo systemctl enable postgresql-9.6

### PostgreSQLユーザーの作成
sudo useradd postgres
sudo passwd postgres
※パスワードは任意

### PosgtreSQLにユーザーとDBを作成
su - postgre
psql
CREATE ROLE test_user WITH LOGIN PASSWORD 'password';
CREATE DATABASE test_db;

下記の値は任意
※ユーザー名:test_user
※パスワード:password
※DB名:test_db

### Vagrantfileに追記
config.vm.network "forwarded_port", guest: 5432, host: 5432, id:"postgres"

### 次回起動
cd C:\Users\ユーザー名\Documents\Work\01_Vagrant\centos7
vagrant up

pgAdminのインストール(必要なら)

下記リンクの「pgadmin4-4.12-x86.exe」をダウンロードしてインストール。
https://www.postgresql.org/ftp/pgadmin/pgadmin4/v4.12/windows/

VisualBasic2019をインストール

下記リンクの必要なものをダウンロードしてインストール。
https://visualstudio.microsoft.com/ja/downloads/?utm_medium=microsoft&utm_source=docs.microsoft.com&utm_campaign=button+cta&utm_content=download+vs2019&rr=https%3A%2F%2Fdocs.microsoft.com%2Fja-jp%2Fvisualstudio%2Fide%2Fwhats-new-visual-studio-2019%3Fview%3Dvs-2019

VisualBasic2019からPostgreSQLに接続

作成したプロジェクトの「参照」を右クリックし、「NuGet パッケージの管理(N)」を選択。
「参照」の検索窓に「npgsql」と入力し、一番上に出てくる「Npgsql」をインストール。

テーブル作成例

VB.NET
Imports Npgsql

Public Class DbTest

    Dim server As String = "localhost"
    Dim port As String = "5432"
    Dim userId As String = "test_user"
    Dim password As String = "password"
    Dim database As String = "test_db"

    Public Sub Create_Database()
        Using conn As New NpgsqlConnection("Server=" + server + "; Port=" + port + "; User Id=" + userId + "; Password=" + password + "; Database=" + database)
            conn.Open()

            Try
                Dim cmd As NpgsqlCommand = New NpgsqlCommand("Create Table sample(id NUMERIC, name TEXT);", conn)
                cmd.ExecuteNonQuery()
            Catch ex As NpgsqlException
                Console.WriteLine(ex)
            End Try

            conn.Close()
        End Using
    End Sub
End Class
1
0
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
1
0