Where can I get Postgresql unique installation ID?
like...
SELECT dbid from v$database; (in oracle)
I need it to identify my different database installation across different sites.
Getting MAC id of server is a way but if there is a way to get it from postgresql it would be preferred.
if you have replication slot available, you can use IDENTIFY_SYSTEM:
MacBook-Air:~ vao$ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
Timing is on.
Pager usage is off.
systemid | timeline | xlogpos | dbname
---------------------+----------+------------+----------
6401169876745218982 | 1 | 1/7A470D80 | postgres
(1 row)
Related
I have Postgres 9.6 installed on mac os. When I enter the command:
$ psql (it takes me to below prompt)
bar=#
bar=# \conninfo
You are connected to database "bar" as user "bar" via socket in "/tmp" at port "5432".
How do I get out of database bar and be at the top level so that when I enter command:
CREATE DATABASE postgis_in_action;
CREATE SCHEMA ch07;
CREATE TABLE ch07.bag_o_rasters(rid serial primary key, rast_name text, rast raster);
database postgis_in_action will be created and within this database ch07 schema will be created and not nested inside database "bar" and the table will be created within ch07 schema under postgis_in_action database?
After creating the new database you need to switch to it. Otherwise the create schema will be run in the database to which you initially connected. In psql you can do that using \connect
bar=# CREATE DATABASE postgis_in_action;
bar=# \connect postgis_in_action
You are now connected to database "postgis_in_action" as user "postgres".
CREATE SCHEMA ch07;
CREATE TABLE ch07.bag_o_rasters(rid serial primary key, rast_name text, rast raster);
I would strongly recommend you create a regular user to do your work. Do not do everything as the superuser. E.g.:
bar=# create user ace password '*******';
bar=# create CREATE DATABASE postgis_in_action owner ace;
\connect postgis_in_action ace
Password for user ace:
You are now connected to database "postgis_in_action" as user "ace".
postgis_in_action=>
Maybe you have an experience with other databases, but this is the Postgres. Schemas are nested in databases, and you cannot to connect to schema (in Postgres). If you want create the database, then you use CREATE DATABASE ch07 instead CREATE SCHEMA ch06.
Instance (Postgres Cluster)
|
v
-------------------- ...
| |
v v
Database1 Database2
|
----------------------- ...
| | |
v v v
public schema1 schema2
|
----------------------- ...
| | |
v v v
table1 table2 table3
In this case the Postgres is similar to MS SQL, and very different to Oracle. Schema in Postgres and Oracle are different things.
When you connect to Postgres, then you have to specify target database. You cannot to connect just to server, or you cannot to connect to schema. Schemas (in Postgres) are like directories. You can specify an order of searching of schemas. You can set SEARCH_PATH per connect, per user or in an session (it is analogy of PATH in MS Win or UNIX).
I exported a database using dokku postgres:export dbname and imported it to another instance using dokku postgres:import dbname. The application running on the new instance does not connect correctly to the new imported database. It is connecting to the database itself, but the data is not there. I entered connected to both instances. Running \l both were identical. However when I run \dt is where the difference is.
Actual Results
// On the new instance
=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------------------+-------+----------
public | document | table | postgres
...
// On the original instance
=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------------------+-------+----------
dbname | document | table | postgres
...
The data was moved over, but is is the public schema (I think). Running select queries gives me different results as well.
// Original machine
SELECT * FROM document;
// Returns the data
SELECT * FROM dbname.document;
// Also returns the data
// New machine
SELECT * FROM document;
// Empty result
SELECT * FROM dbname.document;
// Returns the data
Expected Results
I guess I would expect that exporting from one db and importing to another with the same name would produce nearly identical instances. Any help here would be great. Everything else is working great in the app, it connects to the db, but returns no data.
In case anyone has this problem I got it working by connecting to the postgres instance by running dokku postgres:connect dbname and changing the search_path from "$user", public to dbname, public. It works for now. However I don't feel like this is the best solution.
I am working on this flask based web app. While things are working fine in the app, I wanted to verify the same in my postgres database.
While I can access user data in my app, I am not able to see the same when I try to access while SQL command on the terminal. This problem is specific to the 'user' table, while other tables could be accessed normally on terminal too.
This is the result of my user query:
classroom=# SELECT * FROM user;
user
-------
fatih
(1 row)
and this is some other table in the same database:
classroom=# SELECT * FROM course;
id | title | code
----+-----------------------------+----------
3 | Algorithms | COC2030
4 | Web development using Flask | FLASK001
(2 rows)
I am expecting the same type of result as in the course table, as in the user table.
Assuming there are more columns or rows in the user table that you'd like to see, you could try:
Log in using the same user as the flask app.
Log in as user postgres and check if that changes things (sudo -u postgres psql classroom, or psql classroom postgres.)
Use the \dp command to see if there are column privileges.
Use the \dn+ command to see if there are row security policies.
PostgreSQL has 2 template databases: template0 that can't be modified, and template1 that is modifiable and is used to create every new database.
So I was playing with privileges, granting and revoking, and it caught my attention that, at least in my Docker image (as well as locally installed PostgreSQL), both template database have the same access privileges.
If I read this correct, it is giving connect privilege to both postgres default user and public role.
-- output is simplified
postgres=# \l
List of databases
Name | Owner | Access privileges
template0 | postgres | =c/postgres +
postgres=CTc/postgres
template1 | postgres | =c/postgres +
| postgres=CTc/postgres
Yet I know for sure that trying to connect to template0 will fail with
FATAL: database "template0" is not currently accepting connections.
I did some digging, th catalog pg_database has datallowconn property and in a description it says
it is used to protect the template0 database from being altered.
It might be an answer, but I'm not sure. I mean it's just a catalog, I think it stores data about object, I'm not sure if it can affect the behaviour of an object in any way.
So the ultimate question is - why can't we connect to template0?
The value false in the datallowconn field in pg_database is the only reason that prevents you from connecting to the template0 database. You can remove this lock by simply update the value as Postgres super user:
UPDATE pg_database SET datallowconn = TRUE WHERE datname = 'template0';
Now, you can connect to template0 by:
psql -Upostgres -dtemplate0
But you better not be doing that, of course. The postgres developers will have thought of something when they blocked access. If you corrupt the template databases, it's hard to create a new database.
As a Rails novice, I'm following instructions in railscast #342 to set up a postgres database connection on my Mac.
I created a new rails project with
$ rails new blog -d postgresql
I edited the database yaml file to set the username and password.
I used psql to add the new user and password, and gave it permission to create tables: alter user blog create db
I created the db via
rake db:create:all
It succeeded and inside psql, doing \l to list schemas, I see all three schemas blog_test, blog_development and blog_production
I then do
$ rails g scaffold article name content:text
all looks good
I then do
$ rake db:migrate
I get messages showing success:
$ rake db:migrate
== 20150701220010 CreateArticles: migrating ===================================
-- create_table(:articles)
-> 0.0128s
== 20150701220010 CreateArticles: migrated (0.0129s) ==========================
I set my search path to look at the schema:
set search_path to lcuff,public,blog_development;
show search_path:
search_path
---------------------------------
lcuff, public, blog_development
But trying to find the table,
# \d
No relations found.
I've done the db:migrate VERSION=0 and it successfully reports that it drops the table, and then I create it again with db:migrate and it reports success.
If the first part hadn't worked, where it actually created the schema, I'd think I'm pointed to the wrong database somehow.
Ideas?
You should first connect to the database before fetching the tables.
\connect blog_development
And then try giving \d to list all tables.
You can also try with \dt.
Example(Tested in my Project):
\connect my_db
You are now connected to database "my_db" as user "postgres".
my_db=# \d
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+-------+----------
public | access_managements | table | postgres
public | amenities | table | postgres
public | city_coordinates | table | postgres
public | coapplicants | table | postgres
Source