DB Schema with their Sizes on Postgresql

To Know the list of database schema and their size Run below Sql Query

SELECT schema_name,pg_size_pretty(sum(table_size)::bigint) as “disk space”,(sum(table_size) / pg_database_size(current_database())) * 100 as “percent” FROM (SELECT pg_catalog.pg_namespace.nspname as schema_name,pg_relation_size(pg_catalog.pg_class.oid) as table_size FROM pg_catalog.pg_class JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid) t GROUP BY schema_name ORDER BY schema_name asc

Nodejs as a service with SYSTEMD

Recently working on MEAN (MONGODB, EXPRESSJS, ANGULAR, NODEJS) Stack environment. I enjoy to wotked on application how its eco friendly with web. After finishing my app I want to run on server mongodb and angular was fine to me but question arise how to run nodejs in background as I am using debian10.

So I start with a system service for a Nodejs application to run as long as system is up. In linux system “Systemd” is a service manager which start, stop restart programs.

Create a file nodejsapp.service with the following content on /usr/lib/systemd/system.


[Unit]

Description=Node.js ContactList Http Server[Service]

User=Alok Sah

Group=Alok Group

Restart=always

KillSignal=SIGQUIT

[Service]

WorkingDirectory=/root/contactlist/

Restart=on-failure

ExecStart=/usr/bin/node  /root/contactlist/app.js

[Install]

WantedBy=multi-user.target

assuming my nodejs file is in /root/contactlist/app.js.

Now using systemctl to control our app with the following commands


//whenever any service file is changes use daemon-reload 

sudo systemctl daemon-reload

// enabling this service at machines start

sudo systemctl enable nodejsapp

sudo systemctl disable nodejsapp

// to start the app.js service

sudo systemctl start nodejsapp

sudo systemctl stop nodejsapp

sudo systemctl restart nodejsapp

//check the status of service active 

sudo systemctl status nodejsapp

Once the Nodejs service is started successfully with no configuration errors, launch the web browser and check your application its running without npm start :).

Using “BYTEA” DATATYPE WITH POSTGRESQL

Storing large object files into database is much interesting then saving path of files.
Large object data like images, document can be save into database by two simple method:
1> Save images in base encoded form that i had explained in my earlier article PHP Encoding with base64
2> Use BYTEA Data type in Postgresql.
“The BYTEA Data type allows storage of binary strings” you may say as RAW Bytes.
When you SELECT a Bytea type, PostgreSQL returns octal byte values prefixed with ‘\’ (e.g. \032). Users are supposed to convert back to binary format manually.

To Start with create column doc_image having data type “Bytea” in a table.
Now when you use file upload on your PHP projects, you will get file data in file super-global variable $_FILES
and you first have to move file to your server like i have move files to upload folder


move_uploaded_file($_FILES["file"]["tmp_name"], "/upload/".$_FILES["file"]["name"]);
// get data of image from upload folder
$dataString = file_get_contents("/upload/".$_FILES["file"]["name"]);
$raw_data = pg_escape_bytea($dataString); //Store this string into your databse.

Use “pg_escape_bytea” function, it returns escaped string for insertion into a bytea field., and store this raw string ($raw_data) to database.

Now to retrieve that file data use “pg_unescape_bytea” function,  It returns the unescaped string, possibly containing binary data.

and if you had used  image file to upload then use “pg_unescape_bytea” function to convert into binary and display to browser as

header(‘Content-type: image/jpeg’);
echo pg_unescape_bytea($fileStringfromDB);

mysqldump, An easy way to restore MySQL Database

A few days ago, I have to take my database dump which is in my local system and stored in MySQL, i tried from software tool phpMyAdmin but its take too much time and cannot succeed.
so i thought to restore my database by command line and use MySQL utility “mysqldump”.
I found its very easy and in less then a minutes its take all my databases dump.

To use it traverse to mysql bin directory.
C:\wamp\bin\mysql\mysql5.6.17\bin
to take dump there are various parameters with this command.
for a simple use type the following command

mysqldump -u root -p dbname > C:dbdump1.sql

-u is a database user, -p is for password,  dbname is your database name, “>” this tells mysqldump command to restore given database to this path “C:dbdump1.sql”.

Another example

mysqldump -u[dbuser] -p[password] dbname > dump2.sql

Note: this give you an “Warning: Using a password on the command line interface can be insecure.”

All Database Dump:
if you want to take all of your database dump at on go, below is command

mysqldump --all-databases --single-transaction --user=root  > dump3.sql

if you have set any password for your database then include “–password=dbpassword” to the above command, this is again insecure.

Specify Databases:
include –databases <db1> <db2>, this allow you to take your specific databases dump

mysqldump --databases db1 db2--single-transaction --user=root --password &gt; dump4.sql

🙂



nike tn pas cher tn pas cher peuterey outlet piumini peuterey outlet spaccio peuterey outlet peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online hogan outlet scarpe hogan outlet hogan outlet online doudoune moncler pas cher moncler pas cher doudoune moncler hogan outlet scarpe hogan outlet hogan outlet online nike tn pas cher tn pas cher