You are on page 1of 4

R-PostgreSQL integration

1. Installation of postgresql and creating users, databases and granting


permissions to them:

A. Installation:
a) lsb_release –a #to check the Ubuntu version
b) sudo apt-get update
c) sudo apt-get install postgresql #default version of postgresql
supported by Ubuntu is 9.3
d) sudo su postgres #to switch to postgres user
e) psql –d postgres –U postgres #to log on to postgres database

B. Creating Users:
a) #CREATE USER app_ro WITH PASSWORD ‘mypassword’;
b) #CREATE USER app_rw WITH PASSWORD ‘mypassword’;
c) #ALTER USER app_ro WITH PASSWORD ‘mynewpassword’;

C. Creating Database
 CREATE DATABASE myapp;

i. Switch/Log onto that database


 #\c myapp

ii. Revoking permissions for this database


 #REVOKE ALL ON DATABASE myapp FROM PUBLIC;
 #REVOKE ALL ON SCHEMA public FROMPUBLIC;

iii. To see the created users info


 #\du

D. Granting Permissions :
 #GRANT CONNECT ON DATABASE myapp to app_ro;
 #GRANT CONNECT ON DATABASE myapp to app_rw;
 To see the information of the granted permissions
 #\l
 #GRANT SELECT ON ALL TABLES IN SCHEMA public to app_ro;
 For app_rw user
 #GRANT SELECT,UPDATE,INSERT,DELETE ON ALL TABLES IN
SCHEMA public to app_rw;
 #\l

iv. Creating Tables


 #CREATE TABLE test(id serial,name varchar(40));

v. To see the permissions in the table


 #\z

vi. Granting the permissions to the tables


 #GRANT SELECT ON ALL TABLES IN SCHEMA public to app_ro;
 #\z

vii. To give all the permissions give the following 3 commands for
app_ro user:
 #GRANT SELECT ON ALL TABLES IN SCHEMA public to app_ro;
 #GRANT SELECT ON ALL SEQUENCES IN SCHEMA public to app_ro;
 #GRANT USAGE ON SCHEMA public to app_ro;
 #\z

viii. To give all the permissions give the following 3 commands for
app_rw user:
 #GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA
public to app_rw;
 #GRANT SELECT, UPDATE, INSERT, DELETE ON ALL SEQUENCES IN
SCHEMA public to app_rw;
 #GRANT USAGE ON SCHEMA public to app_rw;
 #\z
 #\q
ix. To add privileges by default, so that we do not need to do the
above 3 steps repeatedly
 $psql –d postgres –U postgres
 #\c myapp
 #ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT
ON TABLES to app_ro;
 # ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT
ON SEQUENCES to app_ro;

x. For app_rw user:


 # ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT,
UPDATE, INSERT, DELETE ON TABLES TO app_rw;
 # ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT,
UPDATE ON SEQUENCES TO app_rw;

xi. Now create a table and see, the privileged are given by default
 #CREATE TABLE test2(id serial,name varchar(40));
 #\z

xii. Inserting values in the table test:


 $sudo su postgres
 #\c myapp
 #insert into test(name) Values(‘test me’);
 # insert into test(name) Values(‘test me too’);
 #select * from test;
 #\q

exit

2. Installing of RPostgreSQL library in R and connecting to the database


created above:

We have already installed R, so open a terminal and type R, to get into the R-
prompt.

A. Installing RPostgreSQL package


 >system(‘gksudo “apt-get –y install postgresql-9.3 libpq-dev” ‘)
 >install.packages(“RPostgreSQL”) //select CRAN as 0.cloud

 >library(RPostgreSQL) //if this command runs without giving any


error, means you have installed RPostgresql package successfully.

We have referred the following YouTube videos for these part:


1. For installing and getting started with PostgreSql:
https://youtu.be/67XGzdzv9k0

2. For Connectivity of R and PostgreSQL:


https://youtu.be/90j5rX6iSGI

You might also like