`psql` command to view all existing tablespaces? - postgresql

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)

Related

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;

psql saying database does not exist but it does exist in pgadmin

I have just installed Postgres v10.4 on Windows and created, using pgadmin, a new database called analysis. It is there, I can see it in pgadmin, and it has one table in it. However, I cannot connect to this database using psql.
C:\WINDOWS\system32>psql -d postgres -U postgres
psql (10.4)
WARNING: Console code page (850) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# l
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-----------------------------+-----------------------------+-----------------------
postgres | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 |
template0 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres-# \q
C:\WINDOWS\system32>psql -d analysis -U postgres
psql: FATAL: database "analysis" does not exist
C:\WINDOWS\system32>
I don't know what I'm doing with psql, but all I've found is that I can connect to db postgres as user postgres. There's a \l command and it shows 3 databases (2 of which, template0 and template1) I can't even see in pgadmin.
How can I connect to the database (analysis) that I've just created in pgadmin? Can anybody explain what user I'm logged in under in pgadmin, if it's different to 'postgres' and if it isn't, how is it that I can't see in psql what I can see in pgadmin?
template0 and template1 are know as skeleton databases. When you use CREATE DATABASE command postgres copy the existing databae.
By default template1 database is used to create new database.
I think your pgadmin and psql are connected to different cluster. A cluster in postgresql is collection of one or more databases in a single instance of server
The image you posted does not show the complete view of pgadmin browser. But
if there are 2 cluster then you can see in pgadmin like "Servers(2)". To find running port of each cluster right click on corresponding cluster and select properties then click connection tab. Here port number can be seen.
Then connect to cluster in psql using that Port.

PostgreSQL can't create user [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 3 years ago.
Issue: I can create users or databases from the shell (bash, OSX) but not postgres cli. From bash I get no confirmation if successful.
If I try to CREATE ROLE in psql then I get no response and it doesn't generate any error. If I try to createuser from bash then if successful it reports back nothing, if unsuccessful then it does generate the error: "role username already exists".
Example:
Yunti-# CREATE ROLE testuser
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Yunti-# CREATE DATABASE testdb
Yunti-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
Yunti | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
template1 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
test | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test5 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)
Yunti-#
A similar thing happens when using createdb.
How can I create users and databases in postgres cli?
And is this normal to get no response to most postgres commands in bash?
Info: users and their privileges:
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Your statements are not executed, because you don't terminate them properly using a ;.
Quote from the manual:
A command is composed of a sequence of tokens, terminated by a semicolon (";").
And in the manual for psql:
At the prompt, the user can type in SQL commands. Ordinarily, input lines are sent to the server when a command-terminating semicolon is reached. An end of line does not terminate a command. Thus commands can be spread over several lines for clarity. If the command was sent and executed without error, the results of the command are displayed on the screen.
If you do that, you get an output like this:
psql (9.4.4)
Type "help" for help.
postgres=# CREATE ROLE testuser;
CREATE ROLE ---<<<< this tells you the statement was executed
postgres=#
I dont know if the postgres client is using other commands in OSX as it is in Linux but I assume it is the same.
This docs link shows some options for the postgres client:
It seems like "\l" lists the databases while the option you would like to see is roles and their access which is "\du".
When creating a database from within the client you should get a response in the form of "CREATING DATABASE". Maybe you are having some sort of syntax error?
I don't think users is created elsewhere.
I hope this solves some of your problems.

Description of PostgreSQL database tables installed on a particular computer

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!

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