Description of PostgreSQL database tables installed on a particular computer - postgresql

A bit over a year ago I installed PostgreSQL on six computers. On one of those computers, I imported 2 gig of census data from CSV files.
Historically I have just worked with flat files, but in this case the files are so big they choke my analysis software. I am new to both PostgreSQL and relational databases in general, and I have a very basic beginner's question: What software (e.g. pgAdmin III) and what and commands would I use to quickly answer the following questions on each machine:
Is PostgreSQL in still installed and running on each machine?
(If 1 is yes) Does the machine in question have installed any non-bundled tables or data?
(If 2 is yes) How can I produce a summary description of the tables that are installed?
In terms of a summary description, I am hoping for the table name, a list of column names, the data type of each, and the number of lines or records in each table, and possibly any additional database-relevant facts like whether the column is a key or indexed.
I work mainly under Windows 7 & 8, though I have a virtual Ubuntu macine installed on one computer.

Is postgreSQL in still installed and running on each machine?
The method you would use to find that out will depend on the operating system on each machine.
On Linux hosts you could use this:
ps -ef | grep postgres
If you see a process named postgres then postgresql is installed and running.
If not, it may be installed but not running. You could check the package management system of your distro to check if it is installed, for example on RPM based systems:
rpm -qa "*postgres*"
On a Windows machine you may be able to see if it is running using the task manager. To check if it is installed go into the Control Panel "Programs and Features" option.
Does the machine in question have installed any non-bundled tables or data?
By non-bundled I assume you mean tables or data other than the system catalogs that are created when you install the system.
My preference is to interact via the command line psql interface. Once you get the psql prompt you can use various 'backslash commands' to inspect the database.
To open the psql command - well you will need appropriate credentials. The details are going to depend on how you configured things when you installed it. If you happen to be using Linux, and you have root access, then the easiest way is to su to the postgres Linux user first, which in most cases will be able to connect directly to the database:
$ sudo su - postgres
$ psql
To see what databases exist use the \l command:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
harmic | harmic | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
postgres | postgres | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
template0 | postgres | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
The databases postgres, template0 and template1 are created by the system so the only database containing user data is 'harmic' in this case.
To connect to a database use \c:
postgres=# \c harmic
You are now connected to database "harmic" as user "postgres".
To list all the tables that exist in this database, including system catalogs use \dt:
harmic=# \dt+ *.*
List of relations
Schema | Name | Type | Owner | Size | Description
--------------------+-------------------------+-------+----------+------------+-------------
information_schema | sql_features | table | postgres | 96 kB |
information_schema | sql_implementation_info | table | postgres | 48 kB |
information_schema | sql_languages | table | postgres | 48 kB |
information_schema | sql_packages | table | postgres | 48 kB |
information_schema | sql_parts | table | postgres | 48 kB |
information_schema | sql_sizing | table | postgres | 48 kB |
information_schema | sql_sizing_profiles | table | postgres | 8192 bytes |
pg_catalog | pg_aggregate | table | postgres | 40 kB |
pg_catalog | pg_am | table | postgres | 40 kB |
... etc
public | aaa | table | harmic | 16 kB |
public | entry | table | harmic | 8192 bytes |
public | exams | table | harmic | 8192 bytes |
(60 rows)
The tables listed in the Schema's 'information_schema' and 'pg_catalog' are not user tables. pg_catalog contains the internal information used by the database to keep track of everything in the database, and information_schema contains information tables about the database which are standardized by the SQL standard. In my case there are a few tables in the 'public' schema that are actual user tables.
How can I produce a summary description of the tables that are installed?
To see a full description of one or more tables:
harmic=# \d public.aaa
Table "public.aaa"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
The above trivial table has two columns named a and b, of types integer and text respectively.
You can use wildcards to get this listing printed for matching tables, eg:
harmic=# \d public.*
You can get an exact number of rows that exists in each table by executing:
SELECT count(1) FROM aaa;
(where aaa would be the table name).
Doing this for each and every table could be slow and tedious. You can get an approximation of the number of rows in all tables by inspecting some tables in the pg_catalog like this:
harmic=# SELECT nspname as schemaname,
harmic-# relname as tablename,
harmic-# reltuples as approx_rows
harmic-# FROM pg_class LEFT JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace WHERE nspname='public' and relkind='r';
schemaname | tablename | approx_rows
------------+-----------+-------------
public | exams | 3
public | entry | 2
public | aaa | 2
(3 rows)
Note that the number of rows shown is updated whenever the database is analysed. The autovacuum daemon does this automatically from time to time, but you can also manually trigger it using command ANALYZE;

Question 1: What OS are you running? If its a sensible one(*nix), try running psql in the terminal of any of them. That will definitely tell you if psql is running. Whether postgres is installed is a totally different question. Determining that is very situation specific (What OS do you have?).
Question 2: Im not familiar with bundling tables. But simple inspection of the GUI provided by pgAdmin would be your best bet. What is bundling?
Question 3: Best way to generate a summary of a series of tables would be to run raw sql :)
The following will answer most of your described needs. Connect to psql and run something like the following:
connect <database_name>
\d (will list all the tables in the database)
\d <tablename> ('describes' the table)
select count(*) from <tablename> (returns the total number of rows in the table.)
This is the best place for postgres stuff: http://www.postgresql.org/docs/
Bloody boring reading, but very simple and to the point.
Best of luck!

Related

relation *db_name* does not exist postgres

I am trying to set up an ubuntu server with postgres(following digitalocean tuorial).
I ssh'd to the server as root user, from there created a user named 'justin', gave that user admin privileges as instructed. Switched to 'justin', installed python, django, pip, etc and postgres. From here the tutorial said type sudo -u postgres psql
, I did this. From here I created a db 'jobzumodb' and user 'jobzumojustin'. I am now trying to grant privileges of that db to this new user: GRANT ALL PRIVILEGES ON jobzumodb TO jobzumojustin; and it returns ERROR: relation "jobzumodb" does not exist. However if I postgres=# \l I see:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
jobzumodb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | admin=CTc/postgres
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
Anyone know what is causing this?
Note: when I created the db I created it with: CREATE DATABASE jobzumoDB;. And was trying to access it for a long time as 'jobzumoDB' and not 'jobzumodb', before learning the \l command and that it saved as 'jobzumodb' because I did not pass it in quotes. Anyway, not sure if this could be having an effect.
Thanks for any help.
Another edit: upon rereading this I realized I am using the 'postgres' user (I see postgres=#), should I be using the 'jobzumojustin' user and jobzumojustin=# ? Also, I checked to see if 'jobzumojustin' exists, it does, but does not have anything listed under 'list of roles and attributes'.
I think you forgot the database keyword. It should be:
GRANT ALL PRIVILEGES ON DATABASE jobzumodb TO jobzumojustin;

`psql` command to view all existing tablespaces?

What's the psql command to view all existing tablespaces?
\l+ displays all existing databases with their configured tablespace, but it won't display tablespaces which have been created but don't yet contain a database.
As documented in the manual, the command to list tablespaces is \db
If you are looking for a command, just enter \? in the psql command line and it will show you all available commands including a short description.
PSQL meta-command
\db+
SQL
SELECT * FROM pg_tablespace;
Here is the psql command that you can use:
postgres=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
------------+----------+----------+-------------------+---------+--------+-------------
pg_default | postgres | | | | 448 MB |
pg_global | postgres | | | | 631 kB |
(2 rows)

Cannot install any language with Postgres 9.2

I've been using Postgres 9.2 on several different servers for some time, but on one specific database on one specific instance of Postgres I can't install any languages.
$ sudo su - postgres
$ psql
postgres=# select * from pg_language;
This shows internal, sql, c, and plpgsql.
If I connect to myDb and try again:
postgres=# \connect myDb
You are now connected to database "myDb" as user "postgres".
postgres=# select * from pg_language;
I only see internal, sql, and c. I have tried installing the language using createlang as shown in their docs and receive an error:
$ createlang plpgsql myDb
createlang: language installation failed: ERROR: language validation function 2247 called for language 13 instead of 1
I can see that plpgsql.so is in the proper place — and it has to be for the postgres database to have it.
This is also not plpgsql specific as I get the same error message with plpythonu. I can install in the postgres database but not the myDb database.
The documentation and forums do not address this issue.
It seems that your pl_language catalog is broken.
You should see the following:
SELECT oid, * FROM pg_language ;
oid | lanname | lanowner | lanispl | lanpltrusted | lanplcallfoid | laninline | lanvalidator | lanacl
-------+----------+----------+---------+--------------+---------------+-----------+--------------+--------
12 | internal | 10 | f | f | 0 | 0 | 2246 |
13 | c | 10 | f | f | 0 | 0 | 2247 |
14 | sql | 10 | f | t | 0 | 0 | 2248 |
(3 rows)
Instead, your entry for the c language (OID 13) seems to have 1 instead of 2247 for lanvalidator. Any idea how that happened? Did you manipulate the catalogs?
See if there are any other differences to the above in your pg_language.
The safe way to proceed would be to pg_dump your database, drop and re-create it and load the dump. That should take care of all catalog manipulations.
If you prefer to live dangerous, you can try the following as superuser:
UPDATE pg_language SET lanvalidator = 2247 WHERE oid = 13;

How to Find all address name from osm that's imported into postgres

How can I get all address name from osm. in the schemas imported into postgres database. I have found. But none of them seem to contain a readily available address, which I can easily select. What can I do?
public | geometry_columns | table | postgres
public | planet_osm_line | table | postgres
public | planet_osm_nodes | table | postgres
public | planet_osm_point | table | postgres
public | planet_osm_polygon | table | postgres
public | planet_osm_rels | table | postgres
public | planet_osm_roads | table | postgres
public | planet_osm_ways | table | postgres
public | spatial_ref_sys | table | postgres
The DB schema you are refering to, is produced by osm2pgsql as part of the OSM rendering stack. So it's tuned for the purpose of drawing maps.
To search for an address (called geocoding) is a completely different use of geodata that needs different DB schemas/datastructures. Furthermore, you need to do preprocess and clean of OSM raw data to create hierachies (e.g. continent-country-county-city-street-housenumber) etc.
At OSM the nominatim tool is used for such kind of purposes and is recommend for (reverse)geocoding.

PostgreSQL: Drop Database but DB is still there [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 2 years ago.
I am new to PostgreSQL and I try to get my head around it. I am familiar to db's and MySQL.
I am trying to delete database, which I created since psql seems to ignore the changes I try to push through Django.
When I execute \l I get the following response:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------------+--------+----------+-------------+-------------+-------------------
postgres | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
test_db | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
template0 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template1 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template_postgis | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
(5 rows)
Now I wan to drop the database "test_db" with
DROP DATABASE test_db
but when I execute \l afterwards, the table is still there and the overview looks like about.
Did you type a ; after the DROP DATABASE test_db? Did PostgreSQL print a response to your command?
I had a similar issue when working on a Rails 6 application in Ubuntu 20.04 with PostgreSQL as my database.
When I run the command:
DROP DATABASE my-db;
The database is dropped successfully, however, the schema for the database is still left.
So when I run the command:
CREATE DATABASE my-db;
And I check the tables in the newly created database, I realized they still contained the same tables as the previously deleted database, even though I have not run any migration.
Here's how I fixed it:
Instead of running the command:
DROP DATABASE my-db;
run the command:
DROP DATABASE IF EXISTS my-db;
This deletes the database and it's corresponding schema.
That's all.
I hope this helps