安装
R version 4.3.0 (2023-04-21) -- "Already Tomorrow"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R是自由软件,不带任何担保。
在某些条件下你可以将其自由散布。
用'license()'或'licence()'来看散布的详细条件。
R是个合作计划,有许多人为之做出了贡献.
用'contributors()'来看合作者的详细情况
用'citation()'会告诉你如何在出版物中正确地引用R或R程序包。
用'demo()'来看一些示范程序,用'help()'来阅读在线帮助文件,或
用'help.start()'通过HTML浏览器来看帮助文件。
用'q()'退出R.
>
install.packages("RPostgreSQL", repos = "https://mirrors.ustc.edu.cn/CRAN/")
异常处理
> install.packages("RPostgreSQL", repos = "https://mirrors.ustc.edu.cn/CRAN/")
将程序包安装入‘/usr/local/lib/R/site-library’
(因为‘lib’没有被指定)
试开URL’https://mirrors.ustc.edu.cn/CRAN/src/contrib/RPostgreSQL_0.7-5.tar.gz'
Content type 'application/octet-stream' length 576481 bytes (562 KB)
==================================================
downloaded 562 KB
* installing *source* package ‘RPostgreSQL’ ...
** 成功将‘RPostgreSQL’程序包解包并MD5和检查
** using staged installation
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for pg_config... no
configure: checking for PostgreSQL header files
configure: Checking include /usr/include.
configure: Checking include /usr/include/postgresql.
configure: Checking include /usr/local/include.
configure: Checking include /usr/local/include/postgresql.
configure: Checking include /usr/local/pgsql/include.
configure: Checking include /usr/local/postgresql/include.
configure: Checking lib /usr/lib.
configure: Checking lib /usr/lib/postgresql.
configure: Checking lib /usr/local/lib.
configure: Checking lib /usr/local/lib/postgresql.
configure: Checking lib /usr/local/postgresql/lib.
gcc -g -O2 -fdebug-prefix-map=/build/r-base-cRDaP8/r-base-4.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -I -L conftest.c -lpq -o pqconftest
conftest.c:1:10: fatal error: libpq-fe.h: No such file or directory
#include <libpq-fe.h>
^~~~~~~~~~~~
compilation terminated.
./pqconftest
./configure: line 1999: ./pqconftest: No such file or directory
system -lpq don't appear to work; use internal
checking for "/libpq-fe.h"... no
configure: creating ./config.status
config.status: creating src/Makevars
** libs
using C compiler: ‘gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0’
gcc -I"/usr/share/R/include" -DNDEBUG -I -fpic -g -O2 -fdebug-prefix-map=/build/r-base-cRDaP8/r-base-4.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c RS-DBI.c -o RS-DBI.o
gcc -I"/usr/share/R/include" -DNDEBUG -I -fpic -g -O2 -fdebug-prefix-map=/build/r-base-cRDaP8/r-base-4.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c RS-PQescape.c -o RS-PQescape.o
In file included from RS-PQescape.c:7:0:
RS-PostgreSQL.h:23:14: fatal error: libpq-fe.h: 没有那个文件或目录
# include "libpq-fe.h"
^~~~~~~~~~~~
compilation terminated.
/usr/lib/R/etc/Makeconf:191: recipe for target 'RS-PQescape.o' failed
make: *** [RS-PQescape.o] Error 1
ERROR: compilation failed for package ‘RPostgreSQL’
* removing ‘/usr/local/lib/R/site-library/RPostgreSQL’
下载的程序包在
‘/tmp/RtmpGIv1bR/downloaded_packages’里
Warning message:
In install.packages("RPostgreSQL", repos = "https://mirrors.ustc.edu.cn/CRAN/") :
安装程序包‘RPostgreSQL’时退出狀態的值不是0
安装libpq-dev
apt install libpq-dev
# apt install libpq-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
建议安装:
postgresql-doc-10
下列【新】软件包将被安装:
libpq-dev
升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 3 个软件包未被升级。
需要下载 219 kB 的归档。
解压缩后会消耗 1,101 kB 的额外空间。
获取:1 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libpq-dev amd64 10.23-0ubuntu0.18.04.1 [219 kB]
已下载 219 kB,耗时 1秒 (274 kB/s)
正在选中未选择的软件包 libpq-dev。
(正在读取数据库 ... 系统当前共安装有 184729 个文件和目录。)
正准备解包 .../libpq-dev_10.23-0ubuntu0.18.04.1_amd64.deb ...
正在解包 libpq-dev (10.23-0ubuntu0.18.04.1) ...
正在设置 libpq-dev (10.23-0ubuntu0.18.04.1) ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
重新执行安装命令
install.packages("RPostgreSQL", repos = "https://mirrors.ustc.edu.cn/CRAN/")
加载包
library(RPostgreSQL)
dbConnect
#建立连接
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
host = "localhost", #主机名,默认localhost
port = '5432', #端口号,默认5432
dbname = 'postgres', #数据库
user = 'postgres', #用户名
password = 'postgres') #安装的时候设的postgresql的密码
dbSendQuery
#SQL操作: 这里引号里写SQL代码
re <- dbSendQuery(con, 'SELECT * FROM public."1-7数据集"')
#保存数据
data <- dbFetch(re)
dbGetQuery
df <- dbGetQuery(con, 'SELECT * FROM public."1-7数据集"')
df