473,739 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find out the schema with psycopg?

I am trying to discover the schema of a PostgreSQL database
programatically at runtime.

I'm using psycopg (I assume that's still the best library). Is there a
way to query the schema other than opening a system pipe like "psql -d
'\d'", "psql -d '\d tablename'", etc.?

DBIAPI 2.0 shows that the Cursor object has a .description method that
describes rows, and there's the Type object. But neither of these
appear to give you table names.

Is there something else I should look at?

Thanks in advance,

-S.

Dec 22 '05 #1
3 10605
su*********@gma il.com wrote:
I am trying to discover the schema of a PostgreSQL database
programatically at runtime.

I'm using psycopg (I assume that's still the best library). Is there a
way to query the schema other than opening a system pipe like "psql -d
'\d'", "psql -d '\d tablename'", etc.?

DBIAPI 2.0 shows that the Cursor object has a .description method that
describes rows, and there's the Type object. But neither of these
appear to give you table names.

Is there something else I should look at?


Yes, but as with so many of these things you'll have to accept it's a
platform-specific (i.e. non-portable) solution, and it requires that you
are running PostgreSQL 7.4 or higher. Under those circumstances you can
query the metadata through the information schema.
import psycopg2 as db
conn = db.connect('dbn ame=billings user=steve password=xxxxx port=5432') curs = conn.cursor()
curs.execute("" "select table_name from information_sch ema.tables .... WHERE table_schema='p ublic' AND table_type='BAS E
TABLE'""") curs.fetchall() [('contacts',), ('invoicing',), ('lines',), ('task',), ('products',),
('project'
,)]


regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 22 '05 #2
Steve Holden <st***@holdenwe b.com> wrote:
su*********@gma il.com wrote:
I am trying to discover the schema of a PostgreSQL database
programatically at runtime.

I'm using psycopg (I assume that's still the best library). Is there a
way to query the schema other than opening a system pipe like "psql -d
'\d'", "psql -d '\d tablename'", etc.?

DBIAPI 2.0 shows that the Cursor object has a .description method that
describes rows, and there's the Type object. But neither of these
appear to give you table names.

Is there something else I should look at?


Yes, but as with so many of these things you'll have to accept it's a
platform-specific (i.e. non-portable) solution, and it requires that you
are running PostgreSQL 7.4 or higher. Under those circumstances you can
query the metadata through the information schema.

<snip>

It's not *that* non-portable - information_sch ema is a standard part
of ANSI SQL and is supported by several RDBMSes.

--
Ben Hutchings
It is easier to write an incorrect program than to understand a correct one.
Dec 24 '05 #3
Ben Hutchings wrote:
Steve Holden <st***@holdenwe b.com> wrote:
su*********@g mail.com wrote:
I am trying to discover the schema of a PostgreSQL database
programatica lly at runtime.

I'm using psycopg (I assume that's still the best library). Is there a
way to query the schema other than opening a system pipe like "psql -d
'\d'", "psql -d '\d tablename'", etc.?

DBIAPI 2.0 shows that the Cursor object has a .description method that
describes rows, and there's the Type object. But neither of these
appear to give you table names.

Is there something else I should look at?


Yes, but as with so many of these things you'll have to accept it's a
platform-specific (i.e. non-portable) solution, and it requires that you
are running PostgreSQL 7.4 or higher. Under those circumstances you can
query the metadata through the information schema.


<snip>

It's not *that* non-portable - information_sch ema is a standard part
of ANSI SQL and is supported by several RDBMSes.

Less non-portable than querying the metadata directly, certainly, and
hopefully increasingly more portable as time goes by. Thanks for
reminding me of this desirable attribute of the information_sch ema. I
was wrong to say "platform-specific".

Other readers may be interested in a python snippet that gives them a
little more information about their tables using the information_sch ema:

from pprint import pprint
curs.execute("" "SELECT table_name FROM information_sch ema.tables
WHERE table_schema='p ublic' AND table_type='BAS E TABLE'""")
tables = (x[0] for x in curs.fetchall() )

for tbl in tables:
print tbl.capitalize( )
print "="*len(tbl )
curs.execute("" "SELECT column_name, data_type,
character_maxim um_length,
numeric_precisi on, numeric_precisi on_radix,
numeric_scale, is_nullable
FROM information_sch ema.columns
WHERE table_name=%s
ORDER BY ordinal_positio n""", (tbl, ))
pprint(curs.fet chall())
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 24 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1864
by: AP | last post by:
Hello, I'm trying to use .NET to validate incoming XML documents against the appropriate schema in our database. I do not want to use the document's schemaLocation attribute to validate for various reasons, but rather check against a schema that I pull from a database and add to the reader's schema collection. I am curious as to why the XmlValidatingReader fires an event when the document is invalid, rather than throwing an exception?...
2
5087
by: Tarren | last post by:
Hi: The problem I am having is when I validate an xml file to a schema, it is erroring out every element. I think this has something to do with me defining/referencing the namespaces. I have searched on the net for a while, but am still confused. Thanks in advance for help. Below is the code I am working with. What am I not referencing/referencing incorrectly? ===================================
0
4679
by: Harold Putman | last post by:
The XmlValidatingReader seems to have changed drastically in .Net Frameworks 1.0 SP3 and beyond with regards to validating XML files that are XML Schemas. Consider the following code: --- class ValidatingSchema { static void Main(string args)
1
5165
by: John A Grandy | last post by:
Visual Studio 2005 RC. I create my first ASP.NET 2.0 Web Site at c:\code\my-app , and when I attempt to compile I receive about 30 errors related to the compiler not being able to find schema information : Message 1 Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:configuration'. C:\code\my-app\Web.Config 10 2 C:\...\my-app\ Message 2 Could not find schema information for the...
0
11269
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema information for the". I am using Cassini as the web server on my PCand I can still run my site from within VWD. Does anyone know what I have done to cause these messages to appear? Could not find schema information for the element...
5
3644
by: Rachel | last post by:
Hi, I currently have several sections pointing to various custom providers inside my Web.conf file. I get multiple messages about not being able to find schema information. ("Could not find schema information for the element 'http://schemas.microsoft.com/.NetConfiguration/v2.0:configSections'") Currently my configuration tag is as follow:
3
1577
by: Neilg | last post by:
Does any know what the messsage "could not find schema information... happens with Visual Studio 2005 and how to fix it? Thanks -- Neil Gott
0
7813
by: jhansen | last post by:
I am getting informational warnings about the following app.config and indicate the following. I am using C# visual studio 2005 and used the Settings.settings to set up my values scoped as application. How do I clear up these informational warnings? The 'requirePermission' attribute is not declared. Could not find schema information for the element 'applicationSettings.' Could not find schema information for the element...
1
1971
by: amir | last post by:
Hi, When compiling a page in VS2005 this morning I received 101 messages regarding schema problems in my web.config file. When I go to view an aspx page in my IIS, IE just displays a blank page. I have the June Atlas CTP installed.
2
3045
by: Mark | last post by:
Hi... I've been trying the .Validate() method on the XmlDocument to validate some xml against a schema, but one thing I noted was that unless the document explicitly declares the schema as a default namespace, Validate() just lets it slide through as a success. Is there a way to Validate() either a) mandating against a particular schema, or at the very least b) supplying a default namespace to be used even when not explicitly declared?
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.