initial
This commit is contained in:
commit
74ae93a96e
8 changed files with 94691 additions and 0 deletions
39
joplin notion extension/Web Development/sql/Redis.md
Normal file
39
joplin notion extension/Web Development/sql/Redis.md
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Redis
|
||||
|
||||
- Getting Started
|
||||
https://www.tutorialspoint.com/redis/redis_environment.htm
|
||||
`redis-server` to initiate
|
||||
|
||||
Then in another terminal
|
||||
`redis-cli `
|
||||
|
||||
```
|
||||
redis 127.0.0.1:6379> ping
|
||||
PONG
|
||||
```
|
||||
|
||||
- Connecting to remote server
|
||||
` redis-cli -h host -p port -a password`
|
||||
- Get server information
|
||||
`info` : when connected to redis shell not in just bash
|
||||
|
||||
- retrieving keys
|
||||
Retrieving All Existing Keys
|
||||
As it turns out, every SET command we issued above created a new, unique key within our Redis database. To get a list of all current keys that exist, simply use the KEYS command:
|
||||
> KEYS *
|
||||
1) "title:1"
|
||||
2) "title:2"
|
||||
3) "title"
|
||||
4) "author:2"
|
||||
5) "author"
|
||||
6) "author:1"
|
||||
By following KEYS with an asterisk (*) – which acts as a wildcard search – we’re asking Redis to retrieve all keys in the system. Thus, we not only see our two original title and author keys but also the four enumerated versions that followed as well.
|
||||
|
||||
The syntax following KEYS can be used to search for specific words or phrases within the key, or the exact match as well. Here we want all keys that contain the text 'title':
|
||||
|
||||
> KEYS *title*
|
||||
1) "title:1"
|
||||
2) "title:2"
|
||||
3) "title"
|
||||
- retrieving keys with its values
|
||||
`for i in $(redis-cli KEYS '*'); do echo $i; redis-cli GET $i; done`
|
||||
269
joplin notion extension/Web Development/sql/SQL.md
Normal file
269
joplin notion extension/Web Development/sql/SQL.md
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
SQL
|
||||
|
||||
https://learnsql.com/blog/sql-basics-cheat-sheet/
|
||||
|
||||
## Joins
|
||||
|
||||
**Example**
|
||||
we have 2 tables named 'first' and 'join'
|
||||
first table
|
||||
|id|name|
|
||||
|--|----|
|
||||
|1|name1|
|
||||
|2|name2|
|
||||
|3|name3|
|
||||
|
||||
Books table
|
||||
|id|user_id|book_name|
|
||||
|--|-------|---------|
|
||||
|1|1|book1|
|
||||
|2|1|book2|
|
||||
|3|1|book3|
|
||||
|4|2|book4|
|
||||
|5|4|book5|
|
||||
|
||||
**Cross product** (first X books)
|
||||
Query
|
||||
```sql
|
||||
select * from first,Books;
|
||||
```
|
||||
|
||||
Output
|
||||
|id|name|id|user_id|book_name|
|
||||
|--|----|--|-------|---------|
|
||||
|1|name1|1|1|book1|
|
||||
|2|name2|1|1|book1|
|
||||
|3|name3|1|1|book1|
|
||||
|1|name1|2|1|book2|
|
||||
|2|name2|2|1|book2|
|
||||
|3|name3|2|1|book2|
|
||||
|1|name1|3|1|book3|
|
||||
|2|name2|3|1|book3|
|
||||
|3|name3|3|1|book3|
|
||||
|1|name1|4|2|book4|
|
||||
|2|name2|4|2|book4|
|
||||
|3|name3|4|2|book4|
|
||||
|1|name1|5|4|book5|
|
||||
|2|name2|5|4|book5|
|
||||
|3|name3|5|4|book5|
|
||||
|
||||
|
||||
|
||||
**Union** ( full outer join ) (first U Books)
|
||||
Query
|
||||
```sql
|
||||
-- for mysql
|
||||
SELECT *
|
||||
FROM first
|
||||
LEFT JOIN Books ON first.id = Books.user_id
|
||||
UNION
|
||||
SELECT *
|
||||
FROM first
|
||||
RIGHT JOIN Books ON first.id = Books.user_id;
|
||||
|
||||
-- for others
|
||||
select * from first FULL JOIN Books
|
||||
ON first.id = Books.user_id ;
|
||||
```
|
||||
Output
|
||||
|id|name|id|user_id|book_name|
|
||||
|--|----|--|-------|---------|
|
||||
|1|name1|1|1|book1|
|
||||
|1|name1|2|1|book2|
|
||||
|1|name1|3|1|book3|
|
||||
|2|name2|4|2|book4|
|
||||
|3|name3||||
|
||||
|||5|4|book5|
|
||||
|
||||
|
||||
|
||||
**Intersection** (inner join) (first ∩ Books)
|
||||
Query
|
||||
```sql
|
||||
select * from first,Books
|
||||
where first.id = Books.user_id;
|
||||
|
||||
-- or
|
||||
|
||||
select * from first JOIN Books
|
||||
ON first.id = Books.user_id ;
|
||||
```
|
||||
Output
|
||||
|id|name|id|user_id|book_name|
|
||||
|--|----|--|-------|---------|
|
||||
|1|name1|1|1|book1|
|
||||
|1|name1|2|1|book2|
|
||||
|1|name1|3|1|book3|
|
||||
|2|name2|4|2|book4|
|
||||
|
||||
|
||||
|
||||
|
||||
**LEFT JOIN** (first U first ∩ Books )
|
||||
Means all of left table and intersection of 2 tables is also included
|
||||
Query
|
||||
```sql
|
||||
SELECT *
|
||||
FROM first
|
||||
LEFT JOIN Books ON first.id = Books.user_id
|
||||
|
||||
-- or
|
||||
|
||||
-- Select all rows from table1 and matching rows from table2
|
||||
SELECT first.*, Books.*
|
||||
FROM first, Books
|
||||
WHERE first.id = Books.user_id
|
||||
UNION ALL
|
||||
-- Select all rows from table1 that have no matching row in table2
|
||||
SELECT first.*, NULL AS id, NULL AS user_id , null as book_name
|
||||
FROM first
|
||||
WHERE first.id NOT IN (SELECT user_id FROM Books);
|
||||
```
|
||||
Output
|
||||
|id|name|id|user_id|book_name|
|
||||
|--|----|--|-------|---------|
|
||||
|1|name1|1|1|book1|
|
||||
|1|name1|2|1|book2|
|
||||
|1|name1|3|1|book3|
|
||||
|2|name2|4|2|book4|
|
||||
|3|name3||||
|
||||
|
||||
|
||||
|
||||
**RIGHT JOIN** (first ∩ Books U Books)
|
||||
Means all of right table and intersection of 2 tables is also included
|
||||
Query
|
||||
```sql
|
||||
SELECT *
|
||||
FROM first
|
||||
RIGHT JOIN Books ON first.id = Books.user_id;
|
||||
```
|
||||
Output
|
||||
|id|name|id|user_id|book_name|
|
||||
|--|----|--|-------|---------|
|
||||
|1|name1|1|1|book1|
|
||||
|1|name1|2|1|book2|
|
||||
|1|name1|3|1|book3|
|
||||
|2|name2|4|2|book4|
|
||||
|||5|4|book5|
|
||||
|
||||
|
||||
|
||||
Triple JOIN
|
||||
Query
|
||||
```sql
|
||||
SELECT h.Address, p.Name, p.Address as OwnerAddress, i.url FROM house AS h
|
||||
INNER JOIN person AS p ON p.personID = h.personID
|
||||
INNER JOIN images AS i ON i.personID = p.personID
|
||||
GROUP BY h.houseID
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
**Some snippets**
|
||||
- query to select those records in which user has login and there session is recorded. Not all users who loginned have their session recorded.
|
||||
Table named `users` have columns `id` as int, `email`
|
||||
Table named `login` have columns like `id` and `email`
|
||||
Table named `sessions` have columns like `id` and `userid` as text
|
||||
Connection between users and login is email and connection between users and sessions is userid. So we need to join these 3 tables as in order to connect `login` and `sessions` we need to connect it with mutual relationship `users`
|
||||
```sql
|
||||
select login.*, sessions.userid from users join login on users.email = login.email
|
||||
join sessions on CAST(users.id as text) = sessions.userid
|
||||
group by login.id, sessions.userid
|
||||
```
|
||||
As sessions.userid is text so we type cased users.id to text when comparing.
|
||||
|
||||
## Keys
|
||||
|
||||
- Adding foriegn key
|
||||
https://www.w3schools.com/sql/sql_foreignkey.asp
|
||||
```sql
|
||||
CREATE TABLE Orders (
|
||||
OrderID int NOT NULL,
|
||||
OrderNumber int NOT NULL,
|
||||
PersonID int,
|
||||
PRIMARY KEY (OrderID),
|
||||
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
|
||||
);
|
||||
```
|
||||
here Persons in the other table where Orders.PersonID is referencing to Persons.PersonID
|
||||
Make sure that foreign key dont contain value which is not available in reference table.
|
||||
- preventing delete related records for foreign keys
|
||||
Cascade Delete: This will automatically delete records in table B when the corresponding record in table A is deleted. `ON DELETE CASCADE`
|
||||
```sql
|
||||
CREATE TABLE A (
|
||||
id INT PRIMARY KEY,
|
||||
-- other fields
|
||||
);
|
||||
|
||||
CREATE TABLE B (
|
||||
id INT PRIMARY KEY,
|
||||
a_id INT,
|
||||
-- other fields,
|
||||
FOREIGN KEY (a_id) REFERENCES A(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
```
|
||||
Set Null: This sets the foreign key in table B to NULL when the referenced record in table A is deleted.
|
||||
`ON DELETE SET NULL`
|
||||
Restrict: This will prevent the deletion of a record in table A if there are any related records in table B.
|
||||
`ON DELETE RESTRICT`. This is default
|
||||
|
||||
- cloning table data or structure
|
||||
- cloning structure only
|
||||
`CREATE TABLE employees_clone LIKE employees;`
|
||||
- cloning structure and data
|
||||
`CREATE TABLE employees_clone AS SELECT * FROM employees;`
|
||||
|
||||
|
||||
Update command
|
||||
```sql
|
||||
UPDATE Customers
|
||||
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
|
||||
WHERE CustomerID = 1;
|
||||
```
|
||||
|
||||
|
||||
|
||||
insert elements if not exists
|
||||
```sql
|
||||
INSERT INTO table_name (column1, column2, column3)
|
||||
SELECT 'value1', 'value2', 'value3'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM table_name
|
||||
WHERE column1 = 'value1' AND column2 = 'value2'
|
||||
);
|
||||
```
|
||||
|
||||
|
||||
update joins
|
||||
https://stackoverflow.com/questions/982919/sql-update-query-using-joins
|
||||
```sql
|
||||
-- Source - https://stackoverflow.com/a/982947
|
||||
-- Posted by gbn, modified by community. See post 'Timeline' for change history
|
||||
-- Retrieved 2026-06-18, License - CC BY-SA 3.0
|
||||
|
||||
UPDATE im
|
||||
SET mf_item_number = gm.SKU --etc
|
||||
FROM item_master im
|
||||
JOIN group_master gm
|
||||
ON im.sku = gm.sku
|
||||
JOIN Manufacturer_Master mm
|
||||
ON gm.ManufacturerID = mm.ManufacturerID
|
||||
WHERE im.mf_item_number like 'STA%' AND
|
||||
gm.manufacturerID = 34
|
||||
|
||||
```
|
||||
if this dont work then using legacy way
|
||||
```js
|
||||
const updateQuery = `update ${songDataTableEnums.tablename} as sd
|
||||
set ${songDataTableEnums.columns.is_bookmarked}=${bookmark_value}
|
||||
where exists (
|
||||
select 1
|
||||
from ${keywordTableEnums.tablename} as k
|
||||
where sd.${songDataTableEnums.columns.id} = k.${keywordTableEnums.columns.song_id}
|
||||
and k.${keywordTableEnums.columns.keywords}=?
|
||||
)`
|
||||
```
|
||||
27
joplin notion extension/Web Development/sql/extras.md
Normal file
27
joplin notion extension/Web Development/sql/extras.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
extras
|
||||
|
||||
- pocketbase : for small projects
|
||||
- Conflict-Free Replicated Data Type: A conflict-free replicated data type (CRDT) is a special distributed data structure that lets multiple computers store, update, and sync copies of data independently without needing a central coordinator or locking mechanism
|
||||
- last write wins
|
||||
|
||||
```txt
|
||||
there is a table name tab and it contains a column name col.
|
||||
|
||||
now this database is in both devices and is same too. values in column 'col' of table 'tab' in both database are A.B,C which is last modified at 5pm
|
||||
|
||||
in device1 changes being made and now its value is X,B . this database is last updated at 6pm
|
||||
in device2 changes being made and now its value is Y,C. this database is last updated at 7pm
|
||||
|
||||
now how to sync the changes?
|
||||
in first one, A is replaced with X, C is deleted
|
||||
in 2nd one, A is replaced with Y, B is deleted
|
||||
|
||||
so total changes are
|
||||
A -> X
|
||||
C -> gone
|
||||
A -> Y
|
||||
B -> gone
|
||||
|
||||
here A being updated to either X and Y is decided by time so latest one ie 7pm one win and A->Y, B and C are deleted
|
||||
so finally it will be Y
|
||||
```
|
||||
190
joplin notion extension/Web Development/sql/mysql.md
Normal file
190
joplin notion extension/Web Development/sql/mysql.md
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
|
||||
- MariaDB
|
||||
https://www.mariadbtutorial.com/getting-started/connect-to-mariadb/
|
||||
|
||||
After installing mariaDB use this command to setup database directory
|
||||
`mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql`
|
||||
do this `rm -rf /var/lib/mysql/*` to undo this initialization process
|
||||
|
||||
To start mariadbd at boot time you have to copy
|
||||
**support-files/mariadb.service** to the right place for your system
|
||||
U can do this in systemd init system with
|
||||
`systemctl enable --now mariadb.service`
|
||||
|
||||
|
||||
Two all-privilege accounts were created.
|
||||
One is **root@localhost**, it has no password, but you need to
|
||||
be system 'root' user to connect. Use, for example, `sudo mysql`
|
||||
The second is **mysql@localhost**, it has no password either, but
|
||||
you need to be the system 'mysql' user to connect.
|
||||
After connecting you can set the password, if you would need to be
|
||||
able to connect as any of these users with a password and without sudo
|
||||
|
||||
See the MariaDB Knowledgebase at https://mariadb.com/kb
|
||||
|
||||
You can start the MariaDB daemon with:
|
||||
`cd '/usr' ; /usr/bin/mariadb-safe --datadir='/var/lib/mysql'`
|
||||
|
||||
You can test the MariaDB daemon with mariadb-test-run.pl
|
||||
`cd '/usr/mariadb-test' ; perl mariadb-test-run.pl`
|
||||
|
||||
Please report any problems at https://mariadb.org/jira
|
||||
|
||||
The latest information about MariaDB is available at https://mariadb.org/.
|
||||
|
||||
Consider joining MariaDB's strong and vibrant community:
|
||||
https://mariadb.org/get-involved/
|
||||
|
||||
- More
|
||||
To load mysql shell in command prompt:
|
||||
|
||||
`mysql -u root -p`
|
||||
Here root is owner_name. This will prompt for password which is ‘password’.
|
||||
or
|
||||
`mysql -h localhost -P 3306 -u root -p test`
|
||||
|
||||
- To connect to remote mysql
|
||||
To connect to MariaDB on a specific host, you use the -h option:
|
||||
|
||||
`mysql -u [username] -p[password] -h [hostname]`
|
||||
or to disable ssl check when connecting
|
||||
`mysql --skip-ssl -u proj1_wishrocky -P 3306 -p94db734d1935c57ac05eab8987a959c98f074706 -h r77rn.h.filess.io proj1_wishrocky`
|
||||
or (for newer versions)
|
||||
` mysql --ssl-mode=DISABLED -u proj1_wishrocky -P 3306 -p94db734d1935c57ac05eab8987a959c98f074706 -h r77rn.h.filess.io proj1_wishrocky`
|
||||
|
||||
|
||||
|
||||
To get list of database:
|
||||
|
||||
`show databases;`
|
||||
|
||||
To create database
|
||||
```sql
|
||||
drop database if exists database_name;
|
||||
create database database_name;
|
||||
```
|
||||
|
||||
To load database
|
||||
|
||||
`use hospitals;`
|
||||
|
||||
hospitals here is name of database
|
||||
|
||||
Once database is loaded, to get list of tables:
|
||||
|
||||
`show tables;`
|
||||
|
||||
To get schema of tables:
|
||||
|
||||
`desc patients;` or `describe patients;`
|
||||
|
||||
where patients is name of table
|
||||
|
||||
- To create table
|
||||
```sql
|
||||
drop table if exists table_name;
|
||||
create table table_name(....);
|
||||
```
|
||||
|
||||
- Creating table
|
||||
|
||||
```sql
|
||||
create table project (
|
||||
emp_id int not null,
|
||||
id int not null,
|
||||
name varchar(30),
|
||||
location varchar(40),
|
||||
primary key(emp_id)
|
||||
);
|
||||
```
|
||||
- Adding Auto increment
|
||||
- SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
|
||||
|
||||
```sql
|
||||
-- To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:
|
||||
ALTER TABLE Persons AUTO_INCREMENT=100;
|
||||
|
||||
alter table employee
|
||||
modify emp_id int not null auto_increment;
|
||||
```
|
||||
- Change mysql password if forget
|
||||
- Stop mysql service
|
||||
`sudo systemctl stop mysql`
|
||||
- start it in safe mode
|
||||
`sudo mysqld_safe --skip-grant-tables --skip-networking`
|
||||
- Then in other terminal type
|
||||
`mysql -u root`
|
||||
- when in mysql shell
|
||||
`FLUSH PRIVILEGES;`
|
||||
`ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';`
|
||||
- close the terminal of the safe mode and start mysql service again
|
||||
`sudo systemctl start mysql`
|
||||
- running sql script in mysql
|
||||
`mysql -u username -p`
|
||||
then
|
||||
`SOURCE /path/to/your/script.sql;`
|
||||
Alternatively, you can execute the script directly from the command line without entering the MySQL shell
|
||||
`mysql -u username -p < /path/to/your/script.sql`
|
||||
- if else use in mysql
|
||||
```sql
|
||||
SET @primaryKeyValue = 13; /* Your primary key value here */
|
||||
|
||||
IF EXISTS (SELECT 1 FROM TableB WHERE foreignKeyColumn = @primaryKeyValue) THEN
|
||||
-- Update the record in TableA
|
||||
UPDATE TableA
|
||||
SET someField = 'newValue' -- Set your field and value accordingly
|
||||
WHERE primaryKeyColumn = @primaryKeyValue;
|
||||
ELSE
|
||||
-- Delete the record in TableA
|
||||
DELETE FROM TableA
|
||||
WHERE primaryKeyColumn = @primaryKeyValue;
|
||||
END IF;
|
||||
|
||||
```
|
||||
- creating and using procedures
|
||||
```sql
|
||||
-- to replace semicolon with // for procedures
|
||||
DELIMITER //
|
||||
|
||||
CREATE PROCEDURE if not EXISTS UpdateOrDeleteQuestion(IN primaryKeyValue INT)
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM quiz_questions WHERE questionID = primaryKeyValue) THEN
|
||||
-- Update the record in questions
|
||||
UPDATE questions
|
||||
SET isDeleted = TRUE
|
||||
WHERE id = primaryKeyValue;
|
||||
ELSE
|
||||
-- Delete the record in questions
|
||||
DELETE FROM questions
|
||||
WHERE id = primaryKeyValue;
|
||||
END IF;
|
||||
END //
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
-- Now you can call the procedure with the primary key value
|
||||
CALL UpdateOrDeleteQuestion(2);
|
||||
-- to delete it
|
||||
drop procedure UpdateOrDeleteQuestion;
|
||||
```
|
||||
|
||||
- to run mysql in docker first setup `/var/lib/mysql` by
|
||||
`mysql_install_db --user=root --basedir=/usr --datadir=/var/lib/mysql`
|
||||
then
|
||||
```bash
|
||||
mysqld_safe --user=root &
|
||||
|
||||
# Wait for MySQL to initialize (this is important)
|
||||
sleep 5
|
||||
|
||||
# other scripts to start (like backend services)
|
||||
# npm start
|
||||
```
|
||||
in order to check if mysql is listening on localhost:3306 try this
|
||||
`netstat -tuln | grep 3306`
|
||||
|
||||
|
||||
|
||||
|id|username|discriminator|avatar|accent_color|banner|theme_colors|pronouns|phone|desktop|mobile|premium|premium_type|bot|bio|system|nsfw_allowed|mfa_enabled|totp_secret|totp_last_ticket|created_at|premium_since|verified|disabled|deleted|email|flags|public_flags|purchased_flags|premium_usage_flags|rights|data|fingerprints|settingsIndex|webauthn_enabled|badge_ids|avatar_decoration_data|display_name_styles|collectibles|primary_guild|
|
||||
|--|--------|-------------|------|------------|------|------------|--------|-----|-------|------|-------|------------|---|---|------|------------|-----------|-----------|----------------|----------|-------------|--------|--------|-------|-----|-----|------------|---------------|-------------------|------|----|------------|-------------|----------------|---------|----------------------|-------------------|------------|-------------|
|
||||
|1471020127947448357|qwerty|3217|68d3b54bae93fdb947e64cb7df2584c5||c587b1be12f1491329d90359d89dc6bf||||false|false|true|2|false||false|true|false|||2026-02-11 11:18:48.735|2026-02-11 11:18:48.735|true|false|false|qwerty@email.com|0|0|0|0|875069521787904|{"hash":"$2b$12$I4fHr7GfGjbVttWsYFdZXue0uN2DMeSKbSAzu77JdZWS7fS4iJUsC","valid_tokens_since":"2026-02-11T05:48:48.735Z"}||1|false||||||
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
postgresql syntax
|
||||
|
||||
- Creating table
|
||||
```sql
|
||||
DROP TABLE IF EXISTS COMPANY;
|
||||
CREATE TABLE COMPANY(
|
||||
ID INT PRIMARY KEY NOT NULL,
|
||||
NAME TEXT NOT NULL,
|
||||
AGE INT NOT NULL,
|
||||
ADDRESS CHAR(50),
|
||||
SALARY REAL
|
||||
);
|
||||
```
|
||||
For auto increment
|
||||
```sql
|
||||
CREATE TABLE COMPANY(
|
||||
ID SERIAL PRIMARY KEY NOT NULL,
|
||||
NAME TEXT NOT NULL,
|
||||
AGE INT NOT NULL,
|
||||
ADDRESS CHAR(50),
|
||||
SALARY REAL
|
||||
);
|
||||
```
|
||||
|
||||
`SERIAL` for auto increment
|
||||
- to load postgresql database
|
||||
|
||||
```bash
|
||||
psql -U postgres smart-brain
|
||||
```
|
||||
|
||||
Here postgres is owner name and smart-brain is name of data base. It will prompt for password which is U2FsdGVkX1/k+uOTLs4gSYVmxYAJEQybNVGnxIQVHyg=. Refer to
|
||||
|
||||
[Web development](../../../undefined)
|
||||
|
||||
for decrypting it.
|
||||
|
||||
To load postgresql without database:
|
||||
|
||||
`psql -U postgres`
|
||||
|
||||
To create database:
|
||||
|
||||
`createdb mydb -U postgres`
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
psql -U postgres
|
||||
create database mydb;
|
||||
```
|
||||
|
||||
To get list of database:
|
||||
|
||||
`\l`
|
||||
|
||||
To connect to database when login as user
|
||||
|
||||
`\connect dbname`
|
||||
|
||||
`\q`: To quit
|
||||
- List tables, views and sequences
|
||||
`\d`
|
||||
- Describe tables
|
||||
`\d <table_name>`
|
||||
240
joplin notion extension/Web Development/sql/postgresql.md
Normal file
240
joplin notion extension/Web Development/sql/postgresql.md
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
postgresql
|
||||
|
||||
- Installation
|
||||
https://www.geeksforgeeks.org/how-to-install-postgresql-on-arch-based-linux-distributions-manjaro/
|
||||
https://gist.github.com/NickMcSweeney/3444ce99209ee9bd9393ae6ab48599d8
|
||||
- To use it
|
||||
`sudo su - postgres`
|
||||
or
|
||||
`psql -U postgres`
|
||||
Creating database when in user
|
||||
`create database <database_name>`
|
||||
delete database
|
||||
`drop database <database_name>`
|
||||
- Remote connection
|
||||
`psql -U postgres -p 5432 -h hostname` (5432 is default postgresql port number)
|
||||
or
|
||||
`psql -U postgres -p 5432 -h hostname -d database_name`
|
||||
-U is username
|
||||
-p for port
|
||||
-h for hostname
|
||||
-d for database name
|
||||
|
||||
To directly connect with url
|
||||
`psql postgres://user:password@ip_add_or_domain:port/db_name`
|
||||
or
|
||||
`PGPASSWORD="your_password" psql -U your_username -d your_database -h your_host`
|
||||
- Import and export
|
||||
Exporting postgresql Database
|
||||
We need to create a dump file. Here smart-brain is name of database
|
||||
`pg_dump -U postgres smart-brain > dump.sql`
|
||||
for remote
|
||||
`pg_dump -U postgres -p 5432 -h hostname -d database_name > dump.sql`
|
||||
|
||||
Importing postgresql Database
|
||||
`psql -U postgres smart-brain < dump.sql`
|
||||
|
||||
for archive compressed backups and restore
|
||||
backup
|
||||
` pg_dump -U synapse_user -p 5432 -h localhost -d -Fc synapse > synapse_backup1.dump`
|
||||
restore
|
||||
`pg_restore -U synapse_user -p 5432 -h localhost -d synapse1 synapse_backup1.dump`
|
||||
- Store query output in a file
|
||||
`psql -d DatabaseName -U UserName -c "SELECT * FROM TABLE" -o /root/Desktop/file.txt`
|
||||
Saving query output in csv file
|
||||
https://stackoverflow.com/questions/1517635/save-pl-pgsql-output-from-postgresql-to-a-csv-file
|
||||
By using a meta command `\copy`
|
||||
`\copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER`
|
||||
- Sometimes there is a error `current transaction is aborted, commands ignored until end of transaction block` then run this query
|
||||
`commit;` to fix this
|
||||
|
||||
insert only if the record with paricular value dont exist
|
||||
|
||||
here unique_column is column with must have unique constraint. it must be a key
|
||||
```sql
|
||||
INSERT INTO table_name (column1, column2)
|
||||
VALUES (val1, val2)
|
||||
ON CONFLICT (unique_column) DO NOTHING;
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- set password of postgres user
|
||||
`ALTER USER username WITH PASSWORD 'newpassword';`
|
||||
|
||||
# Setup Postgresql
|
||||
> run postgresql with systemctl
|
||||
|
||||
### Install postgres
|
||||
|
||||
##### latest
|
||||
`sudo pacman -S postgresql`
|
||||
|
||||
##### specific version
|
||||
> find version & build from source
|
||||
|
||||
### Check version
|
||||
`postgres --version`
|
||||
|
||||
### Confirm psql is not running
|
||||
`sudo systemctl status postgresql` --> should not be running
|
||||
|
||||
### Login as the postgres user
|
||||
> Note: always do this any time you are doing any type of admin work on psql
|
||||
|
||||
`sudo su - postgres`
|
||||
|
||||
### Initialize data directory
|
||||
> Note: default db for psql is /var/lib/postgres/data
|
||||
|
||||
`initdb --locale en_US.UTF-8 -D /var/lib/postgres/data`
|
||||
|
||||
### Logout of **postgres** user
|
||||
`exit`
|
||||
|
||||
### Confirm psql is still not running
|
||||
`sudo systemctl status postgresql` --> should not be running
|
||||
|
||||
### Start psql
|
||||
`sudo systemctl start postgresql`
|
||||
|
||||
### Confirm psql is running
|
||||
`sudo systemctl status postgresql` --> should be active
|
||||
|
||||
## Create user
|
||||
|
||||
### Log into postgres
|
||||
`sudo su - postgres`
|
||||
|
||||
### Create a new user
|
||||
> Note: user can be called anything however if you create a PostgreSQL user with the same name as your Linux username, it allows you to access the PostgreSQL database shell without having to specify a user to login (which makes it quite convenient).
|
||||
|
||||
`createuser --interactive`
|
||||
or
|
||||
`sudo -u postgres bash`
|
||||
`createuser --pwprompt synapse_user`
|
||||
and for db
|
||||
`createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse`
|
||||
|
||||
- Enter name of role to add: `MY_LINUX_USERNAME` (add youur linux user else psql command wont work when using with that user )
|
||||
- Shall the new role be a superuser?: `y`
|
||||
|
||||
### Logout of **postgres** user
|
||||
`exit`
|
||||
|
||||
### Restart psql
|
||||
`sudo systemctl restart postgresql`
|
||||
|
||||
### Confirm psql is running
|
||||
> Note: can see that it was restarted by looking at the timestamp on the ***Active*** field
|
||||
|
||||
`sudo systemctl status postgresql`
|
||||
|
||||
|
||||
### Managing users
|
||||
To login as a newly created user in PostgreSQL, you typically need to use a client or interface that supports PostgreSQL, such as `psql`, PgAdmin, or another tool that allows database interaction. Here's how you can do it using `psql`, the command-line tool for PostgreSQL.
|
||||
|
||||
1. **Create the User**: First, you need to create the user (if you haven't done so already). You can do this by connecting to your PostgreSQL instance as a superuser (often `postgres`) and running a command like the following:
|
||||
First type
|
||||
`psql -U postgres`
|
||||
```sql
|
||||
CREATE USER my_new_user WITH PASSWORD 'password';
|
||||
```
|
||||
|
||||
Adjust `my_new_user` and `'password'` according to your preferences.
|
||||
|
||||
2. **Grant Permissions** (if needed): Depending on what you want this user to do, you'll likely need to grant them some permissions or access to a database. Here's an example of how you can grant all privileges on a database named `my_database` to your new user:
|
||||
|
||||
```sql
|
||||
GRANT ALL PRIVILEGES ON DATABASE my_database TO my_new_user;
|
||||
```
|
||||
|
||||
3. **Login Using `psql`**: To login as the new user from the command line using `psql`, you would use a command like this:
|
||||
|
||||
```bash
|
||||
psql -d my_database -U my_new_user -W
|
||||
```
|
||||
|
||||
- `-d my_database` specifies the database you are connecting to.
|
||||
- `-U my_new_user` specifies the user you are logging in as.
|
||||
- `-W` prompts `psql` to ask for the password of the user. (Note: For security reasons, it's recommended to use `-W` to prompt for the password instead of passing it directly in the command line.)
|
||||
|
||||
4. **Enter the Password**: After executing the above command, you'll be prompted to enter the password for `my_new_user`. Enter the password you have set for this account, and you should be logged in.
|
||||
|
||||
5. **Using Other Tools**: If you are using a GUI-based tool like PgAdmin, the process involves:
|
||||
|
||||
- Open PgAdmin and connect to your PostgreSQL server.
|
||||
- Create a new server connection if your user has access to a different server or database.
|
||||
|
||||
|
||||
|
||||
- To delete users
|
||||
`drop user <username>`
|
||||
|
||||
- Adding password for local connections
|
||||
By default there is `trust` authentication flag in `pg_hba.conf` file so we can login without requiring password. To change that just change trust to md5
|
||||
Location of `pg_hba.conf` : `/var/lib/postgres/data/pg_hba.conf`
|
||||
Other authenticaor flags are
|
||||
- trust allows the connection without a password.
|
||||
- peer uses the operating system user identity to authenticate (common for local connections).
|
||||
- md5 or scram-sha-256 require a password
|
||||
- Changing password
|
||||
`alter user tpost with password 'qwerty';`
|
||||
|
||||
|
||||
- Schema public permission error
|
||||
When creating table u may get error like
|
||||
`ERROR: permission denied for schema public`
|
||||
Then login to superuser ie `postgres`
|
||||
then run this
|
||||
`GRANT USAGE ON SCHEMA public TO tpost;`
|
||||
to grant permission
|
||||
Then restart the postgresql service
|
||||
|
||||
- running sql script in postgresql
|
||||
`psql -U username -d database_name`
|
||||
then
|
||||
`\i /path/to/your/script.sql;`
|
||||
Alternatively, you can execute the script directly from the command line without entering the MySQL shell
|
||||
`psql -U username -d database_name -f /path/to/your/script.sql`
|
||||
|
||||
|
||||
in debian
|
||||
https://neon.tech/postgresql/postgresql-getting-started/install-postgresql-linux#connect-to-the-postgresql-database-server
|
||||
use this to login to psql
|
||||
`sudo -u postgres psql`
|
||||
or
|
||||
```sh
|
||||
sudo -i -u postgres
|
||||
# then
|
||||
psql
|
||||
```
|
||||
or
|
||||
`psql -U postgres -p 5432 -h localhost`
|
||||
for non interactive password use
|
||||
`PGPASSWORD=PASS psql -U postgres -p 5432 -h localhost` or `psql postgresql://postgres:PASS@localhost:5432`
|
||||
|
||||
|
||||
check database
|
||||
Many ways. Du -sh the folder. Dbdump the database, and there's a psql command for all the statistics too
|
||||
SELECT pg_size_pretty(pg_total_relation_size('"<schema>"."
|
||||
or
|
||||
for specific database
|
||||
`SELECT pg_size_pretty(pg_database_size('your_database_name'));`
|
||||
shortcut
|
||||
`\l+ your_database_name`
|
||||
|
||||
for all database
|
||||
```sql
|
||||
SELECT datname AS database_name,
|
||||
pg_size_pretty(pg_database_size(datname)) AS size
|
||||
FROM pg_database
|
||||
ORDER BY pg_database_size(datname) DESC;
|
||||
|
||||
```
|
||||
shortcut
|
||||
`\l+`
|
||||
https://stackoverflow.com/questions/2596624/how-do-you-find-the-disk-size-of-a-postgres-postgresql-table-and-its-indexes
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
psql peer authetication error
|
||||
|
||||
**by llm**
|
||||
Ah, the **"Peer authentication failed for user"** error typically occurs because PostgreSQL is using **peer authentication**, which means it tries to authenticate you based on your OS username (the user you're logged in as) rather than with a password.
|
||||
|
||||
In this case, your system username doesn’t match the `postgres` user, so PostgreSQL is rejecting the connection.
|
||||
|
||||
To fix this, you can change the authentication method in PostgreSQL’s `pg_hba.conf` file to use **password authentication** (or any other method you prefer).
|
||||
|
||||
Here’s how you can fix this:
|
||||
|
||||
### 1. **Edit `pg_hba.conf` file**
|
||||
|
||||
The first thing you need to do is modify the PostgreSQL authentication settings to allow password-based authentication.
|
||||
|
||||
* Find the location of the `pg_hba.conf` file. It’s typically in one of these locations:
|
||||
|
||||
* `/etc/postgresql/<version>/main/pg_hba.conf` (on Debian-based systems like Ubuntu)
|
||||
* `/var/lib/pgsql/data/pg_hba.conf` (on Red Hat/CentOS/Fedora systems)
|
||||
|
||||
* Open the `pg_hba.conf` file using a text editor like `nano` or `vim`:
|
||||
|
||||
```bash
|
||||
sudo nano /etc/postgresql/<version>/main/pg_hba.conf
|
||||
```
|
||||
|
||||
OR
|
||||
|
||||
```bash
|
||||
sudo nano /var/lib/pgsql/data/pg_hba.conf
|
||||
```
|
||||
|
||||
### 2. **Change authentication method**
|
||||
|
||||
Look for lines that look something like this (for local connections):
|
||||
|
||||
```plaintext
|
||||
local all postgres peer
|
||||
```
|
||||
|
||||
This means PostgreSQL is using **peer authentication** for the `postgres` user when connecting locally.
|
||||
|
||||
Change the `peer` method to `md5` (password authentication):
|
||||
|
||||
```plaintext
|
||||
local all postgres md5
|
||||
```
|
||||
|
||||
This will require a password for the `postgres` user.
|
||||
|
||||
### 3. **Reload PostgreSQL**
|
||||
|
||||
After making the changes, you need to reload PostgreSQL to apply them:
|
||||
|
||||
```bash
|
||||
sudo systemctl reload postgresql
|
||||
```
|
||||
|
||||
### 4. **Set a password for the `postgres` user**
|
||||
|
||||
If you haven’t already set a password for the `postgres` user, do so by logging in as `postgres`:
|
||||
|
||||
```bash
|
||||
sudo -u postgres psql
|
||||
```
|
||||
|
||||
Then, inside the `psql` shell, run:
|
||||
|
||||
```sql
|
||||
ALTER USER postgres PASSWORD 'your_password';
|
||||
```
|
||||
|
||||
Replace `'your_password'` with the password you want to set.
|
||||
|
||||
### 5. **Try connecting again**
|
||||
|
||||
Now you should be able to connect using `psql -U postgres` with the password:
|
||||
|
||||
```bash
|
||||
psql -U postgres -h localhost
|
||||
```
|
||||
|
||||
You will be prompted for the password you just set.
|
||||
|
||||
---
|
||||
|
||||
### Recap:
|
||||
|
||||
1. Modify `pg_hba.conf` to use `md5` instead of `peer` for `postgres` user.
|
||||
2. Reload PostgreSQL.
|
||||
3. Set a password for the `postgres` user.
|
||||
4. Try connecting again using `psql -U postgres`.
|
||||
|
||||
Let me know if this works or if you're still running into any issues!
|
||||
62
joplin notion extension/Web Development/sql/sqlite.md
Normal file
62
joplin notion extension/Web Development/sql/sqlite.md
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
sqlite
|
||||
|
||||
- get list of databases : `.database` or `.databases`
|
||||
- get list of tables : `.tables`
|
||||
- get schemas : `.schema` , for particular table : `schema <TABLE_NAME>`
|
||||
- to show field name when using select query, make sure to switch headers on by `.headers on`
|
||||
|
||||
|
||||
ddl commands
|
||||
|
||||
**alter tables**
|
||||
- `ALTER TABLE old_table_name RENAME TO new_table_name;`
|
||||
- `ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;`
|
||||
- `ALTER TABLE table_name ADD COLUMN column_name datatype;`
|
||||
- `ALTER TABLE table_name DROP COLUMN column_name;`
|
||||
- `ALTER TABLE songdata add COLUMN is_bookmarked INTEGER DEFAULT 0`
|
||||
- `ALTER TABLE Employees ALTER COLUMN Status SET DEFAULT 'Active'` (requires new version)
|
||||
- `ALTER TABLE ... ALTER COLUMN ... DROP NOT NULL` (requires new version)
|
||||
**create tables**
|
||||
- foreign key constraint
|
||||
`FOREIGN KEY (lyrics_url) REFERENCES lyricsUrl(lyrics_url)`
|
||||
|
||||
|
||||
dml ones
|
||||
|
||||
**timestamp**
|
||||
https://www.sqlite.org/lang_datefunc.html
|
||||
```sql
|
||||
CREATE TABLE event_log (
|
||||
id INTEGER PRIMARY KEY,
|
||||
event_name TEXT,
|
||||
|
||||
-- Option A: Stores as human-readable TEXT (UTC time)
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
-- Option B: Stores as a Unix INTEGER timestamp (Seconds since 1970)
|
||||
unix_timestamp INTEGER DEFAULT (strftime('%s', 'now'))
|
||||
);
|
||||
```
|
||||
|
||||
```sql
|
||||
insert into event_log (id,event_name, unix_timestamp)
|
||||
values(1,'event1',unixepoch());
|
||||
```
|
||||
|
||||
|
||||
|
||||
upsert
|
||||
```sql
|
||||
INSERT INTO table_name (column1, column2)
|
||||
VALUES (value1, value2)
|
||||
ON CONFLICT (conflict_column)
|
||||
DO UPDATE SET column1 = excluded.column1, column2 = excluded.column2;
|
||||
```
|
||||
|
||||
|
||||
|
||||
auto increment
|
||||
only possible for integer columns declared as primary key
|
||||
```sql
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
```
|
||||
Loading…
Reference in a new issue