Wednesday, September 28, 2016

Remote psql access to postgresql database

If you would like to access a postgresql database installed on a remote machine using psql. Follow these steps to enable it.

1. Modify the /var/lib/pgsql/9.5/data/pg_hba.conf file.

      Change the IPv4 and IPv6 local connections to: host all all all trust

2. Modify the /var/lib/pgsql/9.5/data/postgresql.conf file.

      Uncomment and change listen_addresses = 'localhost' to listen_addresses = '*'.

3. Add a rule to the iptables if the port is not open. In most cases you do not need to do this.


      iptables  -A INPUT -s 0/0 -p tcp --dport <your port> -j ACCEPT

4. On a remote machine. 

      psql -U postgres -h <database host IP address> -p <database port>

      If you don't want to type the IP address each time, you can set a PGHOST=<database host IP address> environmental variable to bypass it.

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.


[Solved] Use a different port or change the port of postgresql-9.5 on CentOS 6

1. Modify the /var/lib/pgsql/9.5/data/postgresql.conf. 

      Uncomment and change listen_addresses = 'localhost' to listen_addresses = '*'.
      Uncomment and change port=5432 to post=<your port>

2. Create or modify the /etc/sysconfig/pgsql/postgresql-9.5 file.

      Add the following to the file.
            PGPORT=<your port>
            export PGPORT 

3. Start the database server.

      su - postgres
      /usr/pgsql-9.5/bin/pg_ctl -D /var/lib/pgsql/9.5/data -l logfile start

4. Launch the psql prompt

      psql -p <your port>
 

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

The system used here is PostgreSQL 9.5 on CentOS 6.6.

As the postgres user, you type psql and it gives you such an error.

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

First, check if your database server is running with the following commands

      ps -ef | grep postgres

      Which would return something like the following if the server is running.

      root      6616  4849  0 14:13 pts/2    00:00:00 su - postgres
      postgres  6617  6616  0 14:13 pts/2    00:00:00 -bash
      postgres  7219     1  0 15:42 pts/2    00:00:00 /usr/pgsql-9.5/bin/postgres
      postgres  7220  7219  0 15:42 ?        00:00:00 postgres: logger process
      postgres  7222  7219  0 15:42 ?        00:00:00 postgres: checkpointer process
      postgres  7223  7219  0 15:42 ?        00:00:00 postgres: writer process
      postgres  7224  7219  0 15:42 ?        00:00:00 postgres: wal writer process
      postgres  7225  7219  0 15:42 ?        00:00:00 postgres: autovacuum launcher process
      postgres  7226  7219  0 15:42 ?        00:00:00 postgres: stats collector process
      postgres  7239  6617  0 15:44 pts/2    00:00:00 ps -ef
      postgres  7240  6617  0 15:44 pts/2    00:00:00 grep postgres

      If it is not running, use the command below to start it.

      /usr/pgsql-9.5/bin/pg_ctl start

If you are using a different port other than the default 5432, use this command to start psql.

      psql -p <port>

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Order Here

Wednesday, September 21, 2016

SQL: Replace part of a string value in a database table - Solved

Assume you have a FRUIT table in your database. It has a column COUNTRIES containing a comma separated string of country names to indicate where you have business of trading this fruit. Now, you are going to trade Mango with a new country, Ruddo, and for some reason, to stop trading Mango with Gutta, you need to update the COUNTRIES value of the Mango record in your FRUIT table to add Ruddo and remove Gutta.

Instead of retyping the whole string, you can use the SQL below to replace Gutta by Ruddo.

Update FRUIT
Set Countries = REPLACE(Countries, "Gutta", "Ruddo")
WHERE FruitName = 'Mango';

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Thursday, September 1, 2016

java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it.

The erro:

java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
        at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
        at java.awt.Window.<init>(Window.java:535)
        at java.awt.Frame.<init>(Frame.java:420)
        at java.awt.Frame.<init>(Frame.java:385)
        at javax.swing.JFrame.<init>(JFrame.java:180)



To fix it

1. Install and config Xming on your desktop. Download Xming from https://sourceforge.net/projects/xming/    Run the executable file to install it.

2. Ensure that the Enable X11 forwarding is checked in your PuTTY Configuration. And the X display location is set to :0.0.


3. On the linux machine

      export DISPLAY=localhost:11.0

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Trace the system calls of a program execution using strace on linux

You can use strace to trace the system calls of execution of any executables. For example:

      strace  mkdir test
      strace java -jar MyProgram.jar

It is a very powerful debug tool for it allows you to see the details of each system calls.

1. Direct the output to a file

      strace -o <output file name> java -jar MyProgram.jar
      or
      strace java -jar MyProgram.jar > <output file name>

2. Trace one or more particular system calls

      strace -e read java -jar MyProgram.jar

      strace -e trace=read,open java -jar MyProgram.jar //It is important that there is no blank space between 'read,' and 'open'.

3. Trace an executing process

      //Find the pid of the process
      ps -ef | grep <your process name>

      //Trace the process
      sudo strace -p <pid> -e open -o processExecute.txt

---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.