I have launched a postgres container. By injecting script.sql at docker entrypoint, I created database, schema, tables and have inserted data into them. Docker Logs says that all table creation and data insertion is successful .
But How can I validate the data insertion? Below commands didn't help
List of relations
Schema | Name | Type | Owner
------------+----------------------+-------+----------
my_db | users | table | postgres
my_db | audit_log | table | postgres
my_db | config | table | postgres
(3 rows)
my_db=# SELECT * FROM my_db.users
my_db-# SELECT * FROM users
my_db-# SELECT * FROM my_db.users;
What is wrong here? Please help.
You should use
\c my_db
to connect to your database. And then:
SELECT * FROM users;
To query your table into your database.
It seems that you understood this part, but everything should be done using postgresql command line, so inside the docker container.
Related
I have exported all databases using pg_dumpall, then I imported them using psql. for the database "command" the are 2 users, "jo" is the owner and "ko" can manipulate all tables(list, edit, delete).
when I connect to the database using the user "jo" psql -U jo -h localhost -d command I can list all tables.
I run the command \z login I get
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+-------+-------+--------------------+-------------------+----------
command| login | table | jo=arwdDxt/jo +| |
| | | ko=arwdDxt/jo +| |
but when I connect using "ko" psql -U ko -h localhost -d command, I run the \dt I get
Did not find any relations.
when I run select * from command.login; I get the data inside the login table, when run select * from login I get the error
ERROR: relation "login" does not exist
LINE 1: select * from login;
How can I list the tables using the user "ko" ?
Simple: put schema command on the search_path and give user ko USAGE permissions on the schema.
I am new in docker, I've created a dockerfile to launch postgresql image and insert data in a new database.
When I run docker it works well in its container, I can see database and tables created.
But When I try to connect from a client postgres, I see the database created but not tables and we dont know why me and my team..
My DockerFile:
# Dockerfile
FROM postgres:9.6.5
RUN localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
ENV LANG fr_FR.utf8
COPY postgresql.conf /
COPY sql/structure.sql /docker-entrypoint-initdb.d/
My file SQL structure.sql:
CREATE DATABASE DB_ALLOT_BOUCHON_NEW;
GRANT ALL PRIVILEGES ON DATABASE DB_ALLOT_BOUCHON_NEW TO postgres;
CREATE Table IF NOT EXISTS public.SUBSCR (
ID BIGINT NOT NULL,
subscriber_name VARCHAR(100) NOT NULL,
sp_array VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO SUBSCR (ID, subscriber_name, sp_array) VALUES (1, 'client_1', 'sp_array client_1 client_2');
INSERT INTO SUBSCR (ID, subscriber_name, sp_array) VALUES (2, 'client_2', 'sp_array client_2 client_2');
Command to build and run my dockerfile:
$ docker build -t allot_psql .
$ docker run -d --name custom_psql_running -p 5467:5432 allot_psql -c config_file=postgresql.conf
$ docker logs custom_psql_running
$ docker run -it --rm --link custom_psql_running postgres psql -h custom_psql_running -U postgres
# postgres=# SELECT * from subscr;
Result in my container:
postgres=# select * from subscr;
id | subscriber_name | sp_array
----+-----------------+----------------------------
1 | client_1 | sp_array client_1 client_2
2 | client_2 | sp_array client_2 client_2
(2 rows)
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)
postgres=# \d*
Invalid command \d*. Try \? for help.
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)
postgres=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+--------+-------+----------+------------+-------------
public | subscr | table | postgres | 8192 bytes |
(1 row)
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | subscr | table | postgres
(1 row)
postgres=# \
Invalid command \. Try \? for help.
postgres=# \q
Everything seems to work well but when I use postgresql client, I can see only the database created but not tables inside.
jdbc:postgresql://localhost:5467/db_allot_bouchon_new
username:postgres
The structure.sql script creates the tables in the default database postgres and not in DB_ALLOT_BOUCHON_NEW. When the container is initialized you are connected to postgres.To quickly fix connect to db_allot_bouchon_new right after you create it
CREATE DATABASE DB_ALLOT_BOUCHON_NEW;
\connect db_allot_bouchon_new
...
This may help as well.
I will suggest to set POSTGRES_DB environment variable in your Dockerfile so this will create DB and Container will treat this DB as a default DB.
POSTGRES_DB
This optional environment variable can be used to define a different
name for the default database that is created when the image is first
started. If it is not specified, then the value of POSTGRES_USER will
be used.
Dockerfile
FROM postgres:latest
RUN localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8
ENV LANG fr_FR.utf8
ENV POSTGRES_DB="db_allot_bouchon_new"
# To set user and pass but better to pass at run time
ENV POSTGRES_USER="appdbuser"
ENV POSTGRES_PASSWORD="123123"
COPY sql/structure.sql /docker-entrypoint-initdb.d/
In the above Dockerfile, this will create user appdbuser.
docker exec -it custom_psql_running bash -c " psql -U appdbuser"
So you will not need to mention these two lines in SQL script as Docker image will do that for you. So better to remove these line.
CREATE DATABASE DB_ALLOT_BOUCHON_NEW;
GRANT ALL PRIVILEGES ON DATABASE DB_ALLOT_BOUCHON_NEW TO postgres;
Warning:
scripts in /docker-entrypoint-initdb.d are only run if you start the
container with a data directory that is empty; any pre-existing
database will be left untouched on container startup. One common
problem is that if one of your /docker-entrypoint-initdb.d scripts
fails (which will cause the entrypoint script to exit) and your
orchestrator restarts the container with the already initialized data
directory, it will not continue on with your scripts.
I have a postgres database with multiple schemas. When I connect to the database from a shell with psql and I run \dt it uses the default connection schema which is public. Is there a flag I can specify or how can I change the schema?
In PostgreSQL the system determines which table is meant by following a search path, which is a list of schemas to look in.
The first matching table in the search path is taken to be the one wanted, otherwise, if there is no match a error is raised, even if matching table names exist in other schemas in the database.
To show the current search path you can use the following command:
SHOW search_path;
And to put the new schema in the path, you could use:
SET search_path TO myschema;
Or if you want multiple schemas:
SET search_path TO myschema, public;
Reference: https://www.postgresql.org/docs/current/static/ddl-schemas.html
\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1. - List tables inside particular schemas. For eg: 'schema1'.
Do you want to change database?
\l - to display databases
\c - connect to new database
Update.
I've read again your question.
To display schemas
\dn - list of schemas
To change schema, you can try
SET search_path TO
if you in psql just type
SET schema 'temp';
and after that \d shows all relations in "temp
Use schema name with period in psql command to obtain information about this schema.
Setup:
test=# create schema test_schema;
CREATE SCHEMA
test=# create table test_schema.test_table (id int);
CREATE TABLE
test=# create table test_schema.test_table_2 (id int);
CREATE TABLE
Show list of relations in test_schema:
test=# \dt test_schema.
List of relations
Schema | Name | Type | Owner
-------------+--------------+-------+----------
test_schema | test_table | table | postgres
test_schema | test_table_2 | table | postgres
(2 rows)
Show test_schema.test_table definition:
test=# \d test_schema.test_table
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Show all tables in test_schema:
test=# \d test_schema.
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Table "test_schema.test_table_2"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
etc...
This is old, but I put exports in my alias for connecting to the db:
alias schema_one.con="PGOPTIONS='--search_path=schema_one' psql -h host -U user -d database etc"
And for another schema:
alias schema_two.con="PGOPTIONS='--search_path=schema_two' psql -h host -U user -d database etc"
key word :
SET search_path TO
example :
SET search_path TO your_schema_name;
quick solution could be:
SELECT your_db_column_name from "your_db_schema_name"."your_db_tabel_name";
if playing with psql inside docker exec it like this:
docker exec -e "PGOPTIONS=--search_path=<your_schema>" -it docker_pg psql -U user db_name
PostgreSQL 14 Debian
postgres#ovhswift:~$ psql
psql (14.0 (Debian 14.0-1.pgdg100+1))
Type "help" for help.
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create schema tests;
CREATE SCHEMA
test=# \dt
Did not find any relations.
test=# create table pubtable (id integer);
CREATE TABLE
test=# create table tests.schematable (id integer);
CREATE TABLE
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | pubtable | table | postgres
(1 row)
test=# \dt tests.
Did not find any relation named "tests.".
test=# \dt tests
Did not find any relation named "tests".
test=# \dt 'tests.'
Did not find any relation named "tests.".
test=# \dt 'tests.*'
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
tests | schematable | table | postgres
(1 row)
test=# \dt 'tests*'
Did not find any relation named "tests*".
test=# \dt 'tests.*'
List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
tests | schematable | table | postgres
(1 row)
Ditto for \dv etc. to see the views in the schema
I'm wondering why \d only lists tables in the public schema? I have another schema in the database, sps, but those tables are not listed...
# psql -p 5432 -U postgres -h localhost myDB
Password for user postgres:
psql (9.1.5)
Type "help" for help.
myDB=# \d
List of relations
Schema | Name | Type | Owner
--------+----------------------------+----------+----------
public | tableA | table | postgres
public | tableB | table | postgres
public | tableC | table | postgres
public | table_col_seq | sequence | postgres
(4 rows)
You need to change your search_path. In psql use \dn and then build your search_path:
SET search_path = schema1,schema2,public;
I had this problem i Intellij IDEA Database tab.
I got the soultion doing :
Data Sources and Drivers -> YOUR_DATA_SOURCE -> Schemas -> Check "All databases"
I'm a newbie to PostgreSQL.
I'm trying to create a new role named pgdba like below...
localhost.localdomain:[/home/postgres]createuser -d -i -l -P -r -s pgdba
new role's password : pgdba
retype password : pgdba
password : postgres
localhost.localdomain:[/home/postgres]psql
Password: postgres
psql (9.0.1)
Type "help" for help.
postgres=# select * from pg_shadow;
usename | usesysid | usecreatedb | usesuper | usecatupd | passwd | valuntil | useconfig
----------+----------+-------------+----------+-----------+-------------------------------------+----------+-----------
postgres | 10 | t | t | t | md53175bce1d3201d16594cebf9d7eb3f9d | |
pgdba | 16385 | t | t | t | md53cd688326933adcdedd77097c95d131d | |
(2 rows)
postgres=# \q
localhost.localdomain:[/home/postgres]psql -U pgdba
Password for user pgdba: pgdba
FATAL: database "pgdba" does not exist
psql: FATAL: database "pgdba" does not exist
What am I doing wrong?
See how the message is that database pgdba does not exist?
By default, PostgreSQL will connect to a database named after the current user (see the psql docs. Since you aren't specifying a database to connect to, it's trying to connect to a DB named pgdba - which doesn't exist.
Try:
psql -U pgdba postgres
to connect to the postgres database using role pgdba.
You may also need to examine pg_hba.conf (see docs if you wish to tweak the authentication rules.
BTW, you are running an outdated PostgreSQL version with known bugs. Please update to 9.0.8, the latest patch release in the 9.0 series. You do not need to dump and reload your database or use pg_upgrade for this, it's a simple drop-in replacement. See the release notes for all the fixes you're missing.
Also, rather than SELECTing from pg_shadow, consider using psql's user information functions \du and \dg, which produce much better formatted and integrated information.