Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xa7Ai-0005Rz-1i for pgsql-admin@arkaria.postgresql.org; Fri, 03 Oct 2014 18:01:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xa7Ah-0003Rk-HB for pgsql-admin@arkaria.postgresql.org; Fri, 03 Oct 2014 18:01:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xa7Ag-0003Rd-El for pgsql-admin@postgresql.org; Fri, 03 Oct 2014 18:01:38 +0000 Received: from tamriel.snowman.net ([2001:4830:167b::11]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xa7AW-0003cT-O2 for pgsql-admin@postgresql.org; Fri, 03 Oct 2014 18:01:37 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 31FA15F85D; Fri, 3 Oct 2014 14:01:28 -0400 (EDT) Date: Fri, 3 Oct 2014 14:01:28 -0400 From: Stephen Frost To: "Ferrell, Denise CTR NSWCDD, Z11" Cc: "pgsql-admin@postgresql.org" Subject: Re: FW: Creating database links Message-ID: <20141003180128.GO28859@tamriel.snowman.net> References: <2BC18916DE4BF141AC1EFC55CE2CAE7D2B02B9@NAEANRFKXM01V.nadsusea.nads.navy.mil> <4063.1400771439@sss.pgh.pa.us> <2BC18916DE4BF141AC1EFC55CE2CAE7D2B13F2@NAEANRFKXM01V.nadsusea.nads.navy.mil> <2BC18916DE4BF141AC1EFC55CE2CAE7D5817E5@NAEANRFKXM01V.nadsusea.nads.navy.mil> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="suYNV1AwziNMnBoj" Content-Disposition: inline In-Reply-To: <2BC18916DE4BF141AC1EFC55CE2CAE7D5817E5@NAEANRFKXM01V.nadsusea.nads.navy.mil> X-Editor: Vim http://www.vim.org/ X-Info: http://www.snowman.net X-Operating-System: Linux/3.11.0-19-generic (x86_64) X-Uptime: 13:44:02 up 178 days, 4:03, 7 users, load average: 0.06, 0.07, 0.11 User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org --suYNV1AwziNMnBoj Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Denise, * Ferrell, Denise CTR NSWCDD, Z11 (denise.ferrell.ctr@navy.mil) wrote: > What would be the syntax for extracting all columns from a database table= using dblink? Could this be used if you are unsure of the structure of th= e table you're extracting from? To grab all columns from a remote table, you could use: select row_to_json(apples) from apples; This would return a json blob, but unfortunately you can't trivially convert that into a record at this time. You could turn it into a set of key/value pairs though: select b.* =66rom (select row_to_json(data.*) from data) as a, lateral json_each(a.row_to_json) as b; You could write a PL/pgsql procedure to query the remote catalog and determine what the columns are and create the view using dblink accordingly too. To get the columns for a database table, you can query pg_attribute, with pg_class, like so: -- Get the column names for the 'apples' table -- attnum > 0 avoids internal PG columns select attname =66rom pg_attribute join pg_class on (attrelid =3D oid) where relname =3D 'apples' and attnum > 0; You would need to include pg_namespace if you might have an 'apples' table in multiple schemas. What would probably be interesting to you also is the new IMPORT FOREIGN SCHEMA capability which was recently added to PG and will be in PostgreSQL 9.5: http://www.postgresql.org/docs/devel/static/sql-importforeignschema.html Or, perhaps, just using the postgres_fdw and foreign data tables instead of using dblink at all might work. Thanks! Stephen --suYNV1AwziNMnBoj Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJULuR3AAoJEO1sijiDR2RVMnAP/At40h2ETjcvfvb89c/LAKVu bP3WlRAYIRvM5tUAjgI5+UdNw9vV+P9Wnh8Pa839sRgGlneoRudoksRdrGQQYeIa Pw1RR5BoWOCrXJuiRmgdMYGmW8LJZxRsM+W0mGibWlOwi78W6LkoVYFsLRNszRjg c4IA7PFjd74GKafa+ezxX9i3Fi7hD2ir0PcjdMCZRfZ8drmGTQ15APy8wDPxFE26 1d2TzdJRdc7Bypbr/LImn5Je6eSyxJH3kdAYfhcvTPefR19W656v2S73gQjC8EMb FG73IvZHWwsDKbjaNVnvgjzzEAeNPSrPIsM2Gp500NsnQiUvwj3GEFC9/bd4iXYE 4HlrtZkkr6/mP//ptYZOU60NMi+MuDnoARI9iUGkySQlZAj+IoVIYNnnyBHngJxx LyZWt7nKw20GvCYnYTA2lF0c9kzyYxdrRI1qcLRHXS1kRL13ROn6duXojkTvPTcH RtbTjA47oFf4TH9gqkYio07JMrYvEw3xTu6AXovw3qAYVrAjxDmCh5h7Rly07WLo sTMmKQW9AHzgd1DnknBkiKi2hkr7xiQL87l7UTeFFJdeNqG2EgIyRrswDgmP5lVS JFWwvIqCA8SfbD3xGv04Ny+ly+uj058RgtMvEgENPgqYr8R2MK+cwjZ41QhlX1+l 4+mlmGvHHLWEEcDe18my =2JJu -----END PGP SIGNATURE----- --suYNV1AwziNMnBoj--