服务器已完成postgresql 数据库部署,本地电脑用dbeave 数据库管理软件可以连接成功。现在需要用Excel里的vba连接服务器部署的postgresql 数据库。如何连接?本地电脑还需下载安装什么驱动程序吗?
1.注册pgoledb.dll文件
下载软件:pgoledb.msi,下载地址:http://download.csdn.net/detail/zhoushengchao/6456945
2.打开excel vba 代码窗口,使用如下连接字符串即可
Provider=PostgreSQL OLE DB Provider;Data Source=192.168.1.188;location=postgres;User ID=postgres;password=cdsafe;
3.需要开通postgresql的远程访问功能
修改postgresql.conf
listen_addresses = '*'postgreSQL 启用远程访问.txt
修改pg_hda.conf
启用 host all all 0.0.0.0/0 md5
(1)下载和安装PostgreSQL及驱动
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
https://www.enterprisedb.com/download-postgresql-binaries
https://www.postgresql.org/ftp/odbc/versions/msi/
http://www.pgoledb.com/index.php?option=com_filecabinet&view=files&id=1&Itemid=68
(2)代码访问
Private Sub Command1_Click()
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.Open "Driver=PostgreSQL Unicode;Server=127.0.0.1;Port=5432;Database=databasename;Uid=username;Pwd=password;"
rs.Open "select * from tablename", con, 3, 1
Set DataGrid1.DataSource = rs
End Sub
(3)连接字符串
PgOleDb requires a PQLib of version 7.4 or up and it also requires a backend of version 7.4 or up. Timestamps are only guarenteed to work with backends of version 8.0 and up.
Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;
location=myDataBase;User ID=myUsername;password=myPassword;
Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;
location=myDataBase;User ID=myUsername;password=myPassword;timeout=1000;
Provider=PostgreSQL OLE DB Provider;Password=root;User ID=postgres;Data Source=localhost;Location=postgres;
Provider=MSDASQL.1;Persist Security Info=False;User ID=test;Data Source=PostgreSQL30;