Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit c911382

Browse files
committed
Postgres database setup.
1 parent 6a8405f commit c911382

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

databases/README_postgresql.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Database Setup
2+
3+
## Install Packages
4+
5+
Install postgres db, client
6+
7+
```bash
8+
sudo apt install postgresql postgresql-client
9+
```
10+
11+
Python: Install library required for Python's pyscopg2
12+
13+
```bash
14+
sudo apt install libpq-dev
15+
```
16+
17+
Python: Install Python3 psycogp2
18+
19+
```bash
20+
pip3 install psycopg2 --user
21+
```
22+
23+
## Configure Database
24+
25+
Switch to postgres user and open postgres prompt
26+
27+
```bash
28+
sudo su - postgres
29+
psql
30+
```
31+
32+
### Create a new database
33+
34+
List databases
35+
36+
```bash
37+
postgres=# \l
38+
```
39+
40+
Create database
41+
42+
```sql
43+
postgres=#
44+
create database testdb;
45+
```
46+
47+
Connect/use the new database
48+
49+
```bash
50+
postgres=# \c testdb
51+
```
52+
53+
### Create a new table
54+
55+
Describe all tables in the database
56+
57+
```bash
58+
postgres=# \d
59+
```
60+
61+
Create table
62+
63+
```sql
64+
testdb=#
65+
CREATE TABLE COMPANY(
66+
ID SERIAL PRIMARY KEY,
67+
NAME TEXT NOT NULL,
68+
AGE INT NOT NULL,
69+
ADDRESS CHAR(50),
70+
SALARY REAL
71+
);
72+
```
73+
74+
Describe specific table
75+
76+
```bash
77+
postgres=# \d company
78+
```
79+
80+
Display all rows in the company table
81+
82+
```sql
83+
testdb=#
84+
select * from company;
85+
```
86+
87+
### Insert data into the table
88+
89+
Insert some new data
90+
91+
```sql
92+
testdb=#
93+
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Paul', 32, 'California', 20000.00);
94+
95+
insert into company (name,age,address,salary) values ('Robert', 10, 'Moms Basement', 40000.00);
96+
```
97+
98+
### DB User Setup
99+
100+
Create a user, grant full access to the new test database and tables
101+
102+
```sql
103+
create user myuser with password 'mypassword';
104+
grant all on DATABASE testdb TO myuser;
105+
grant ALL on TABLE company TO myuser;
106+
# id_seq table auto created due to "serial" data type of the primary key
107+
grant ALL on TABLE company_id_seq TO myuser;
108+
```
109+
110+
To connect as that user:
111+
112+
```bash
113+
testdb=# \q
114+
sudo su - postgres
115+
psql -h localhost -U myuser testdb
116+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy