Sunday, July 5, 2020

Check if PostgreSQL is running on UBUNTU - Start/Stop

There are commands within ubuntu to check if your postgresql is running or not. Based on its status, you can either start or stop. 

Check if postgresql is running, run following cmd
root@DESKTOP:~# sudo /etc/init.d/postgresql start
10/main (port 5432): down
The output of the above cmd shows the postgresql version and port and status. So the status down means that the server is not running, lets start the server, use following command

root@DESKTOP:~# sudo /etc/init.d/postgresql start
 * Starting PostgreSQL 10 database server
So now you can connect to your postgresql. Inorder to stop the server use following command
root@DESKTOX-QMQVNCE:~# sudo /etc/init.d/postgresql stop
 * Stopping PostgreSQL 10 database server

Wednesday, June 17, 2020

Reset PostgreSQL master password ubuntu bash

I recently lost my admin password of ubuntu postgresql, this is how I did a reset to access my postgresql again

Step 1 : Run following command change the authentication method in the configuration file and then reload
    
sudo sed -ibak 's/^\([^#]*\)md5/\1trust/g' pg_hba.conf
/etc/init.d/postgresql reload

Step 2 : Once the authentication method is reset then you can login to postgres using following and set the new password value

psql -U postgres
postgres=# alter user postgres with password 'NEW_PASSWORD';
postgresl=# \q

Step 3 : Once the new password is set, change the authentication method back to md5
sudo sed -i 's/^\([^#]*\)trust/\1md5/g' pg_hba.conf
sudo -u postgres pg_ctl reload or /etc/init.d/postgresql reload

You are all set to login using your new password

Thursday, June 4, 2020

MySQL delete duplicate

One of the biggest problem with analysis or business intelligence is clean data. In many real world scenarios you will find that data is corrupt, missing or duplicate. In this article we are going to demonstrate multiple ways to delete duplicate data from a table. 

Step 1: Lets create a dummy table. 

create table demo (id int, name char(10))

Step 2: Insert duplicate values

insert into demo(id,name) values(1,'abc');
insert into demo(id,name) values(2,'abd');
insert into demo(id,name) values(3,'vijay');
insert into demo(id,name) values(4,'vijay');
insert into demo(id,name) values(5,'ajay');
insert into demo(id,name) values(6,'sanjay');

Step 3: Select * from demo;

select * from demo;
+------+--------+
| id   | name   |
+------+--------+
|    1 | abc    |
|    2 | abd    |
|    3 | vijay  |
|    4 | vijay  |
|    5 | ajay   |
|    6 | sanjay |
+------+--------+
6 rows in set (0.00 sec)
So the table now contains duplicate values, id 3,4 with name vijay is duplicate. Now lets look at the options to delete this duplicate value

Step 4: Find duplicate value using select

select id, count(name) from demo group by name having count(name)>1;
+------+-------------+
| id   | count(name) |
+------+-------------+
|    3 |           2 |
+------+-------------+
1 row in set (0.00 sec)
So the output shows that id 3 with name vijay has two entries. The simple way to find this was using group by and count on column where you think duplicate are there and should not be. Lets delete this

Step 5: Delete duplicate using inner join

delete d1 from demo d1 inner join demo d2 where d1.id>d2.id AND d1.name=d2.name;
Query OK, 1 row affected (0.00 sec)
Step 6: Check if the query ran successfully, repeat step 4.
select id, count(name) from demo group by name having count(name)>1;
Empty set (0.00 sec)

So no output that means duplicate data has been deleted success. Reconfirm with select on table
select id,name from demo;
+------+--------+
| id | name |
+------+--------+
| 1 | abc |
| 2 | abd |
| 3 | vijay |
| 5 | ajay |
| 6 | sanjay |
+------+--------+
5 rows in set (0.00 sec)

In next article, I am going to show other queries to delete duplicate.

Wednesday, May 27, 2020

Oracle to PostgreSQL using Ora2PG

In this article, we are going to talk about using ora2pg to migrate the database from Oracle to PostgreSQL. We are going to consider PostgreSQL in Azure VM, PostgreSQL and Azure Database for PostgreSQL as target from Oracle. Source Oracle version is 11g XE.

The website https://ora2pg.darold.net/documentation.html talks about manual installation of ora2pg which is complex, there is an easy way to install in on Linux & Windows all you have to do is download the respective scripts for Linux or Windows and run it as root or administrator.


Once the installation is done, the very first step would be to assess your Oracle and create a report

 

Thursday, May 21, 2020

Drop Database in Oracle

Steps to drop a container database in oracle

Step 1 : Connect to the database as sysdba
bash#$ sqlplus / as sysdba

Step 2 : Shutdown the database
SQL> shutdown immediate;

Step 3 : Start mount the database with exclusive restrict to avoid the database being opened
SQL> startup mount exclusive restrict;

Step 4 : Drop database
SQL> drop database;

Wednesday, May 6, 2020

PSQL Commands

To connect to postgresql using psql, update port, dbname, user and your_password in cmd below
psql "host=localhost port=5432 dbname={your_database} user=user password={your_password} sslmode=require"

To import data dump from .sql file using psql, use following cmd
psql > \i 'path-to-dump.sql'
Having single quote around the path is important else you may see C:: permission denied error. If using sudo 
sudo -u postgres psql db_name < 'file_path'  
Create user 
postgres=# create user user_name password 'password';  
Create database 
postgres=# create database mydb owner user_name ;
Install PostgreSQL Client on Ubuntu
bash:=# apt install postgresql-client- ;
To Quit
bash:=# \q;
Help
bash:=# \h;
To describe attributes of a table.
bash:=# \d tablename ;
To get list of databases.
bash:=# \l ;
To connect to a database.
bash:=# \c databasename ;
To list all details of a database.
bash:=# \z  or select * from pg_tables where schemaname = 'information_schema';

Saturday, May 2, 2020

Installing mariadb-server on Ubuntu using sudo


This article demonstrate steps to install MariaDB/MySQL on Ubuntu using sudo. This is part of our getting started journey to learn MariaDB/MySQL. I will be covering PostgreSQL in another article.

Before we get started, very first step would be to install MariaDB/MySQL Server and client. You can install any of these RDBMS on Windows but this blog will mostly be following Ubuntu OS.

There are various tools to access MariaDB/MySQL Server, we are going to use mysqlclient and Heidisql.

Lets get started with installation of MariaDB.

Step 1: Open your Ubuntu terminal window or bash and run following cmd. This step is going to check if mariadb-server is already installed and is going to remove it. You can either continue using the current setup or go to next step to do a fresh installation
sudo apt-get remove mariadb-server

Step 2: run
sudo apt-get install software-properties-common

Step 3: Check for Ubuntu updates
sudo apt-get updates

Step 4: Install Ubuntu updates
sudo apt-get upgrade

Step 5: Install mariadb-server and client. If you just want to install mariadb-server or mariadb-client, just remove the other from the cli.
sudo apt-get install mariadb-server mariadb-client

Step 6: Start/Stop/Restart mariadb-server
service mysql start/stop/restart

Step 6: Connect to mariadb-server using client, run below and enter your password to connect.
mysql -u root -h localhost -p

Getting Started with MariaDB

In our previous post, we demonstrated steps to install MariaDB Server on Ubuntu, in this article, we are going to show how to create database in MySQL/MariaDB. We are going to talk about DDL(Data Definition Language), how to create tables and authorize users to access them.

Start your mariadb-server and follow the steps

Creating Databases, Tables, and Authorized Users

Step 1: Connect to your mariadb-server and run below sql. This can be done via mysql client or any other tools like Heidisql, phpmyadmin etc. This is going to create a new database in your server. 
CREATE DATABASE myFirstDB;
Check if the database already exists and create
CREATE DATABASE IF NOT EXISTS myFirstDB;
To list all the databases in a server.
SHOW DATABASES;
To connect to your database
USE DATABASE_NAME;
Once you have selected your database, use following to create user
CREATE USER 'username'@'localhost' IDENTIFIED BY 'user_password';
To create a user that can connect from any host, use the '%' wildcard as a host part:
CREATE USER 'username'@'%' IDENTIFIED BY 'user_password';
No you can login to your MariaDB using the new user and password
mysql -h localhost -P port -u username -p user_password