/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.core.metadata.datasource.dialect;

import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.metadata.datasource.DataSourceMetaData;
import lombok.Getter;

import java.net.URI;

/**
 * Data source meta data for PostgreSQL.
 *
 * @author panjuan
 */
@Getter
public final class PostgreSQLDataSourceMetaData implements DataSourceMetaData {
    
    private static final int DEFAULT_PORT = 5432;
    
    private final String hostName;
    
    private final int port;
    
    private final String schemeName;
    
    public PostgreSQLDataSourceMetaData(final String url) {
        URI uri = getURI(url);
        hostName = uri.getHost();
        port = -1 == uri.getPort() ? DEFAULT_PORT : uri.getPort();
        schemeName = uri.getPath().isEmpty() ? "" : uri.getPath().substring(1);
    }
    
    private URI getURI(final String url) {
        String cleanUrl = url.substring(5);
        URI result = URI.create(cleanUrl);
        if (null == result.getHost()) {
            throw new ShardingException("The URL of JDBC is not supported.");
        }
        return result;
    }
    
    @Override
    public boolean isInSameDatabaseInstance(final DataSourceMetaData dataSourceMetaData) {
        return hostName.equals(dataSourceMetaData.getHostName()) && port == dataSourceMetaData.getPort();
    }
}

@@ -674,16 +674,55 @@
 alect;%0A%0A
+import com.google.common.base.Strings;%0A
 import i
@@ -881,15 +881,58 @@
 ava.
-net.URI
+util.regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0A%0A/
@@ -1273,125 +1273,296 @@
    p
-ublic PostgreSQLDataSourceMetaData(final String url) %7B%0A        URI uri = getURI(url);%0A        hostName = uri.getHost(
+rivate final Pattern pattern = Pattern.compile(%22jdbc:postgresql://(%5Ba-zA-Z0-9%5C%5C-%5C%5C.%5D+):?(%5B0-9%5D*)/(%5C%5Cw+)%22);%0A    %0A    public PostgreSQLDataSourceMetaData(final String url) %7B%0A        Matcher matcher = pattern.matcher(url);%0A        if (matcher.find()) %7B%0A            hostName = matcher.group(1
 );%0A 
@@ -1572,325 +1572,168 @@
     
+ 
+   
 port = 
--1 == uri.getPort() ? DEFAULT_PORT : uri.getPort();%0A        schemeName = uri.getPath().isEmpty() ? %22%22 : uri.getPath().substring(1
+Strings.isNullOrEmpty(matcher.group(2)) ? DEFAULT_PORT : Integer.valueOf(matcher.group(2)
 );%0A    
-%7D%0A
     
-%0A
     
-private URI getURI(final String url) %7B%0A        String cleanUrl = url.substring(5);%0A        URI result = URI.create(cleanUrl);%0A        if (null == result.getHost())
+schemeName = matcher.group(3);%0A        %7D else
  %7B%0A 
@@ -1823,39 +1823,16 @@
   %7D%0A
-        return result;%0A
     %7D%0A  
   %0A 
@@ -1827,18 +1827,16 @@
     %7D%0A  
-  
 %0A    @Ov

modify PostgreSQLDataSourceMetaData